Skip to content

Instantly share code, notes, and snippets.

@mconnell
Created February 8, 2010 14:22
Show Gist options
  • Save mconnell/298172 to your computer and use it in GitHub Desktop.
Save mconnell/298172 to your computer and use it in GitHub Desktop.
### Basic routes ###
Rails 2:
# singular resource
map.resource :account
# resources with addition member & collection actions
map.resources :games, :member => { :download => :get }, :collection => { :favourites => :get }
Rails 3:
# singular resource
resource :account
# resources with addition member & collection actions
resources :games do
get :download, :on => :member
get :favourites, :on => :collection
end
# If you have multiple member/collection actions they can be grouped via blocks
resources :games do
member do
get :download
put :refresh_achievements
end
collection do
get :favourites
get :wish_list
get :owned_by_friends
end
end
### Defining root mappings ###
Rails 2:
# http://localhost:3000/
map.root :controller => 'posts', :action => 'index'
# http://localhost:3000/admin
map.namespace :admin do |admin|
admin.root :controller => 'posts'
end
Rails 3:
# http://localhost:3000/
root :to => 'Posts#index'
# http://localhost:3000/admin
namespace :admin do
root :to => "Admin::Posts#index"
end
# OR
# http://localhost:3000/admin
namespace :admin do
root :to => "admin/posts#index"
end
One thing to note is that if you are defining a root mapping in a namespace, it doesn't make any assumptions that the controller is in the same namespace in Rails 3.
### Optional params ###
Rails 2:
# Allows a mapping to be associated with multiple routes eg:
# http://localhost:3000/posts/2010
# http://localhost:3000/posts/2010/01
map.connect 'posts/:year/:month', :controller => 'posts', :month => nil, :requirements => { :year => /\d{4}/ }
Rails 3:
# Allows a mapping to be associated with multiple routes eg:
# http://localhost:3000/posts/2010
# http://localhost:3000/posts/2010/01
match 'posts/:year(/:month)' => 'Posts#index', :constraints => { :year => /\d{4}/ }
You'll notice that this mapping also makes use of the :constraints option rather than :requirements which would be found in a Rails 2 app.
@rymawby
Copy link

rymawby commented Oct 23, 2012

Thanks for this - I've forked it for reference - always handy to have on hand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment