I have work on project with ruby on rails and i want to use friendly_id gem. When I install friendly_id gem in my project then add following code according to instructions.
controller:
def show
@group = Group.friendly.find(params[:id])
end
Model:
extend FriendlyId
friendly_id :title, use: [:slugged]
I am using rails version 3.2.11 and friendly_id gem is 4.0.0
Getting error in controller. i do not understand why its happening.
error:
undefined method `slug' for #<Group:0x000000092ac228>
Update :
Previous issue solve with restart server and migrating database.
but it generates new issue Couldn't find group with id=new-demo-group
It seems like you need to update the records that are already created, in your case that would be (Group model), as docs says:
If you're adding FriendlyId to an existing app and need to generate slugs for existing users, do this from the console, runner, or add a Rake task:
Group.find_each(&:save)
Rails console
Open terminal on the app root and run (-e is the environment)
rails console -e production
You should get a similar output to this (I added the command to run):
Running via Spring preloader in process 5389
Loading production environment (Rails 5.1.5)
2.5.1 :001 > Group.find_each(&:save)
Rails runner
Open terminal on the app root and run (-e is the environment)
rails runner -e production "Group.find_each(&:save)"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.