I was wondering if it is possible to route to something like this /:user_id user_id is a custom id that doesn’t just use integers it uses other characters like so NM-001. Then in my controller I have @user = User.find(params[:user_id]). Then in view <%= @user.name %>
I was wondering if it is possible to route to something like this /:user_id
Share
Yes, you can have such a route. However, if your
:user_idwill contain periods then you’ll want to includein the route options to keep Rails from trying to interpret the
.whateverpart of the:user_idas a format specifier.Then, you’ll get
params[:user_id]in your controller and you can turn that into an object however you want. You’d probably want to do what mischa said in the comments:Also, if you really want to use
/:user_idas your route, you’ll want to make sure that none of your userids match any of your present or future top-level routes.