I have a route for one of my resources:
resources :categories
and I need to generate link to category with key property instead of id so I use this:
category_path(category, :id=>category.key)
which generates this
/categories/blah.1
where blah is desired key and 1 is undesired id. How can I awoid that dot with id any why it appears there?
One way to fix it permanently, which doesn't require you to have
category_path(category, :id=>category.key)all the places in your views is that you can create an instance method in yourCategoryclass(app/models/category.rb):Basically,
to_paramis the method which is called in Rails at the time of generating a model object to URL. We've just overridden it here. Now, you don't have to writecategory_path(...), you can just do:and it'll use
keyby default. Changecategory.namewith your appropriate value which you want to show in the link, it was just an example.Note: Make sure you always validate
keyattribute to be present and unique in yourCategoryclass, so that you don't run into invalid/no category being found in the controller at the time of find query.