Friendly-id: Undefined movie slug method

I downloaded the friendly_id gem to make my URLs more user friendly. To follow their instructions, I ask about it here, not about GitHub.

Here is my display method

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

This is consistent 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 the 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.

Of course, I haven't created the migration yet, because I already created the table.

I'm not sure what my next step should be?

Thank you for your help.

+4
source share
3 answers

You need to perform the migration to add the slug column to the table:

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

rake db:migrate, rails Move.find_each(&:save), slug.

+12

, slug.

, FriendlyID friendly_id_slugs, sluggable_id sluggable_type.

create_table "friendly_id_slugs", force: :cascade do |t|
t.string   "slug",                      null: false
t.integer  "sluggable_id",              null: false
t.string   "sluggable_type", limit: 50
t.string   "scope"
t.datetime "created_at"
t.index ["slug", "sluggable_type", "scope"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope", unique: true
t.index ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type"
t.index ["sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_id"
t.index ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type"
end

friendly_id_slugs . __Avoiding_404_s_When_Slugs_Change: http://norman.imtqy.com/friendly_id/file.Guide.html#History__Avoiding_404_s_When_Slugs_Change

+1

, :

class CreateMovies < ActiveRecord::Migration
  def change
    create_table :movies do |t|
      ...
      t.string :slug
      t.index :slug, unique: true, using: :btree
    end
  end
end
0

All Articles