0

I follow the Railscast Episode Pretty URLs with FriendlyId.

I'm on Rails4 and using the 5.0 stable branch on Friendly Id's Github Repo.

I followed the Tutorial:

My Model Clip.rb:

extend FriendlyId
friendly_id :title, use: :slugged

My Migration:

rails g migration AddSlugToClips slug:string

Edited the Migration:

class AddSlugToClips < ActiveRecord::Migration
  def change
    add_column :clips, :slug, :string
    add_index :clips, :slug
  end
end

With an empty Database i try to add a Clip and when i try to open it:

enter image description here

What am i missing?

If i directly access the Vine via

http://localhost:3000/clips/1

I get to the show page..

Mini John
  • 7,855
  • 9
  • 59
  • 108

2 Answers2

3

Ok i found it, it has to do with the Version of Friendly Id you are using.

Finders are no longer overridden by default. If you want to do friendly finds, you must do Model.friendly.find rather than Model.find.

Mini John
  • 7,855
  • 9
  • 59
  • 108
2

It is because when you edit an clip and alter its title its slug will be updated. So try to run the code with following changes.

class Clip < ActiveRecord::Base
  extend FriendlyId
  friendly_id :title, use: :slugged

  def should_generate_new_friendly_id?
    new_record?
  end
end


friendly_id :title, use: :slugged # you must do MyClass.friendly.find(params[:id])
#or
friendly_id :title, use: [:slugged, :finders] # you can now do MyClass.find(params[:id])
Amit Sharma
  • 3,427
  • 2
  • 15
  • 20
  • That is only if i EDIT the TITLE of a clip. But on my instance i upload a new clip and get the error. – Mini John Sep 03 '13 at 12:02
  • Have you tried this friendly_id :title, use: :slugged # you must do MyClass.friendly.find('bar') – Amit Sharma Sep 03 '13 at 12:09
  • Hey amit. I finally found out what was wrong. Check out my Answer :) – Mini John Sep 03 '13 at 12:11
  • Nice, please try to use the find method with my edited answer. – Amit Sharma Sep 03 '13 at 12:18
  • As of version 5.0, FriendlyId uses semantic versioning. One of the most important changes where that Finders are no longer overridden by default. If you want to do friendly finds, you must do Model.friendly.find rather than Model.find. – Mini John Sep 03 '13 at 12:24
  • Nice, Instead of using `@clip = Clip.find_by_slug(params[:id])` use `friendly_id :title, use: [:slugged, :finders]` `@clip = Clip.find(params[:id])` because if you are using Rails4 then try to avoid the use of find_by methods. – Amit Sharma Sep 03 '13 at 12:25
  • I gave you a usefull answer point for trying. Thank you, but for a fact those are the OLD Friedly ID Functions that in Rails4 we dont have to recall. :) – Mini John Sep 03 '13 at 12:28
  • You are right @The Mini John, I am just saying that we can use the find method by adding :finders addon. – Amit Sharma Sep 03 '13 at 12:36