Reputation: 4464
I want to create param /users/will-smith
, so here's my code:
def to_param
"#{full_name.parameterize}"
end
Parameterize will convert "Will Smith" to "will-smith"
So in the controller, the param won't match the find
statement, thus return nil
# User with full_name "will-smith" not found
@user = User.find_by_full_name(params[:id])
Most of the solutions I found is by changing the param to #{id}-#{full_name.parameterize}
. But I don't want the URL to contain the ID.
The other solutions is adding permalink
column in the database, which I don't really like.
Any other solution?
Thanks
Upvotes: 0
Views: 307
Reputation: 1510
Here's a gem called FriendlyId. It will give you more options to play with.
The problem with your code is that you need to convert that parameter back to original, or use the same kind of transformation on your column during the search. FriendlyId, basically, helps you to achieve the same effect.
Also, I'm not sure, but you could miss that gist. It contaits lots of info on the topic.
Upvotes: 2