Converting a route from Rails 2 to Rails 3
I was having some trouble converting the following route from rails 2 way to the new rails 3 way.
map.connect ':departments/:show/:id', :id =开发者_JS百科> /\w+(,\w+)*/
Any help would be appreciated.
Unless :departments and :show are parameter names, I believe you mean 'departments/show/:id'
If that's the case, try this.
match "departments/show/:id" => "departments#show"
On the regex bit, I've never quite done something like that, but I'm sure that it's similar.
First, you should show us what problem you have. Any error message?
The following line should work well in your case:
match ":departments/:show/:id" => "departments#show", :id => /\w+(,\w+)*/
I suspect that the line shown by Adam Eberlin (the upvote goes there!) would be better, because I am a little unsure whether you really need the parameters 'departments' and 'show' (you have placed the colons in front of them).
Your original route was lacking the required parameters: controller and action. I suspect that your route was not working even in Rails 2, or you had some 'defaults' specified, which you just haven't shown us.
The format of constraints did not change, but if you want to be as specific as possible, you may use the :contraints parameter:
match ":departments/:show/:id" => "departments#show",
:constraints => {:id => /\w+(,\w+)*/}
Also, you may see the guide: "Rails Routing from the Outside In".
精彩评论