I’m using this to_param
def to_param
[id, [first_name.parameterize,last_name.parameterize].join("_")].join("-")
end
to produce an address that look like
http://www.example.com/users/34-joe_smith
but if I change the address to
http://www.example.com/users/34-joe_notsmith
It still directs me to user with id 34.
How can I enforce it to go to 404 page or root_path?
Since your search is being performed on the id and not the user’s name it won’t really care what value succeeds the id in the url. You can extend your show action to find the user based on id, first name and last name but I don’t think that’s going to end up being a very clean solution.
Instead, I highly recommend you take a look at the Friendly_ID gem which will enable you to generate pretty URLs with non-pk fields such as user’s name easily with very little configuration. There’s an excellent railscast on the subject: pretty urls with friendlyid