SQLTerminal.app
now available

XPath.app
now available
Want fresh tech tips in your inbox?

Renaming Routes in Rails 3 with :As and :Controller

       

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

config/routes.rb

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.

config/routes.rb

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

app/views/*/.erb

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.

http://pastie.org/1154956

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

Apps I've Built

XPath Expression Editor
Practice and improve your XPath skills with XPath Editor
Click to buy on the App Store
Photo Location Changer
Easily change the location on your photos and videos
Click here for more info about the app
Photo Date Changer
Easily change the dates and times on your photos and videos
Click here for more info about the app
VocabReminder
English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
VocabQuiz
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app