Rn2dy
Rn2dy

Reputation: 4190

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 routes
...
PUT /users/:id/rename/:code(.:format) {:code=>/\w{5}/, :action=>"rename", :controller=>"users"}
...

Upvotes: 0

Views: 625

Answers (1)

bricker
bricker

Reputation: 8941

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

Upvotes: 1

Related Questions