0

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.

gdfgdfg
  • 3,181
  • 7
  • 37
  • 83

1 Answers1

0

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
7urkm3n
  • 6,054
  • 4
  • 29
  • 46
  • I have this column. With `find` method it works fine. The problem is with `find_by_friendly_id` method which I want to use, because it works only with `slug` for parameter, but not the `id` – gdfgdfg Nov 12 '17 at 12:45
  • @gdfgdfg why do u want to use `find_by_friendly_id `, and updated my answer ? – 7urkm3n Nov 13 '17 at 06:02
  • I want because with `find_by_friendly_id`, if you have user/id url (which id is user id) and you pass this id like an argument to the method you will get no results, because it works only with slug. `find` method otherwise works with both - id and slug. If you pass id to find method you will get result. This means user can understand what is his id, which is what I don't want. I have slug column, string type in this table. – gdfgdfg Nov 13 '17 at 13:18
  • For now I am using `find_by_slug` method provided from `Rails`. – gdfgdfg Nov 13 '17 at 13:19