Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

friendly-id: Undefined method slug for Movie

I downloaded the friendly_id gem in order to make my URLs more user friendly. To honor their instructions, I am asking this here instead of on GitHub.

Here is my Show Method

def show
  @movie = Movie.friendly.find(params[:id])
end

This is in compliance with their documentation

Finders are no longer overridden by default. If you want to do friendly finds, you must
do Model.friendly.find rather than Model.find. You can however restore FriendlyId 
4-style finders by using the :finders addon:

In my Model.rb file, I have the following

extend FriendlyId
friendly_id :title, use: :slugged

From their documentation

friendly_id :foo, use: :slugged # you must do MyClass.friendly.find('bar')

also from their documentation

def set_restaurant
  @restaurant = Restaurant.friendly.find(params[:id])
end

For reference, here is their guide.

Granted, I haven't generated a migration yet, because I had already created the table.

I'm unsure of what my next step should be?

Thank you for your help.

like image 551
Johnson Avatar asked Dec 01 '22 15:12

Johnson


1 Answers

You need to run a migration to add the slug column to your table:

class AddSlugToMovies < ActiveRecord::Migration
  def change
    add_column :movies, :slug, :string, unique: true
  end
end

Run rake db:migrate, and then in your rails console run Move.find_each(&:save) to populate the slug column.

like image 104
Marina Avatar answered Dec 04 '22 05:12

Marina