Rail3 routes [edit problem]
Example I have had configured my routes as
match '/cellphones/:permalink/:charger', :controller => 'mycontroller', :action开发者_如何学运维 => 'myaction'
resources :cellphones
Everything ok when I put something like this localhost/cellphones/nokia3323/lion
but I can't edit a cellphone because have the same structure localhost/cellphones/edit/4
Routes that are defined earlier take precedence, so you could make the edit route accessible again by reversing the order that you've declared your routes in. Since a route of /cellphones/edit/:id
is more restrictive than /cellphones/:permalink/:charger
, the edit route will match if the second part of the route is "edit" and pass through to your other route if it's something else.
However, you most likely don't actually have a /cellphones/edit/:id
route, because what's created by resources :cellphones
is /cellphones/:id/edit
which is much harder to distinguish from /cellphones/:permalink/:charger
since both have the wildcard part of the route as the second segment.
The easiest way the fix the problem would be to change the /cellphones/:permalink/:charger
route so it's easier to distinguish. You could use something like /cellphones/p/:permalink/:charger
("p" for permalink), or anything else that's easy to distinguish from the RESTful routes created by resources
.
There's a few other ways you could approach it as well, such as using segment contraints or adding more restful actions.
I found the solution, just had to change the order
resources :cellphones
match '/cellphones/:permalink/:charger', :controller => 'mycontroller', :action => 'myaction'
精彩评论