how to define rails 3.1 custom routes
I want to route http://localhost:3000/users/1/rename/alex
to my users
controller with rename
action.
what I did was:
match 'users/:id/rename/:name' => 'users#rename'
, but this is not working, the part after 'users/:id/'
is not mapped at all, since I cannot get name by params[:name]
Update: In routes.rb
resources :users do
put 'rename/:code', :action => :rename, :code => /\w{5}/, :on => :member
end
and,
$ rake rou开发者_C百科tes
...
PUT /users/:id/rename/:code(.:format) {:code=>/\w{5}/, :action=>"rename", :controller=>"users"}
...
If you have resources :users
, put your match
line before it.
Alternatively, you can pass a block to resources
:
resources :users do
match 'rename/:name' => 'users#rename', :on => :member
end
精彩评论