A RoR v3 routing question?
I was wondering if it is possi开发者_如何学Cble 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 %>
Yes, you can have such a route. However, if your :user_id
will contain periods then you'll want to include
:constraints => { :user_id => /.*/ }
in the route options to keep Rails from trying to interpret the .whatever
part of the :user_id
as 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:
@user = User.find_by_user_id(params[:user_id])
Also, if you really want to use /:user_id
as your route, you'll want to make sure that none of your userids match any of your present or future top-level routes.
精彩评论