Home undefined method `find_by_friendly_id' with FriendlyId gem and Rails 5.1
Reply: 1

undefined method `find_by_friendly_id' with FriendlyId gem and Rails 5.1

gdfgdfg
1#
gdfgdfg Published in 2017-11-11 21:52:16Z

I want to prevent selecting user with his id and that's why I am using this method which should work in this way, but instead I am getting this error:

undefined method `find_by_friendly_id' ...

This is the user model:

include FriendlyId
friendly_id :name, :use => [:slugged, :finders]

and the controller:

def show
    @user= User.find_by_friendly_id(params[:slug])
end

I have column slug in the table.

I am using latest version of the gem - 5.1.0.

7urkm3n
2#
7urkm3n Reply to 2017-11-13 06:06:51Z

Do not forget to generate and database slug

#rails generate friendly_id
#rake db:migrate

.model

extend FriendlyId
friendly_id :permalink, use: [:slugged, :history, :finders]

.controller

@post = Post.find(params[:id])

UPDATED:

I use permalink personally as a sting column.

validates_presence_of :permalink
validates_uniqueness_of :permalink, case_sensitive: false
validates :permalink, presence: true, uniqueness: true, :length => {minimum: 4}

def should_generate_new_friendly_id?
    permalink_changed?
end
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.318111 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO