Easily build a great vocabulary without studying! Now on the App Store!
Click here for more info about the app

Renaming Routes in Rails 3 with :As and :Controller


Renaming routes in Rails 2.* is straight forward. It goes something like this.


map.resources :users, :as => :members

This will give you users_path and form_for(User.new) helpers, for instance, mapping the url to /members instead of /users while using the users_controller.rb class.

In Rails 3.*, this is still possible. It is accomplished differently. In my opinion, it seems a little less elegant.


resources :members, :as => :users, :controller => :users


link_to "Members", users_path form_for User.new do |f|

Rake routes

   users GET    /members(.:format)           {:controller=>"users", :action=>"index"}
   users POST   /members(.:format)           {:controller=>"users", :action=>"create"}
new_user GET    /members/new(.:format)       {:controller=>"users", :action=>"new"}

edit_user GET /members/:id/edit(.:format) {:controller=>"users", :action=>"edit"} user GET /members/:id(.:format) {:controller=>"users", :action=>"show"} user PUT /members/:id(.:format) {:controller=>"users", :action=>"update"} user DELETE /members/:id(.:format) {:controller=>"users", :action=>"destroy"}

Your resource is "members" and you are overriding the name (using :as=>"users") however, you still have to specify the controller.


Tagged w/ #edge #rails 3 #routes #tipsruby on rails