#203 Routing in Rails 3
Rails 3 sports a new routing interface. In this episode I show how to translate the old interface into the new one and show off a few new features.
- Download:
- source codeProject Files in Zip (89 KB)
- mp4Full Size H.264 Video (21.1 MB)
- m4vSmaller H.264 Video (14.3 MB)
- webmFull Size VP8 Video (41.3 MB)
- ogvFull Size Theora Video (28.9 MB)
Resources
- Episode 151: Rack Middleware
- Generic Actions in Rails 3
- How to Build Sinatra on Rails 3
- The Rails 3 Router: Rack it Up
- Rails Routing from the Outside In
- Full episode source code
bash
rails detour
mate detour
rails g controller info about
rails s
rails detour mate detour rails g controller info about rails s
ruby
Detour::Application.routes.draw do |map|
# map.resources :products, :member => { :detailed => :get }
resources :products do
get :detailed, :on => :member
end
# map.resources :forums, :collection => { :sortable => :get, :sort => :put } do |forums|
# forums.resources :topics
# end
resources :forums do
collection do
get :sortable
put :sort
end
resources :topics
end
# map.root :controller => "home", :action => "index"
root :to => "home#index"
# map.about "/about", :controller => "info", :action => "about"
match "/about(.:format)" => "info#about", :as => :about
match "/:year(/:month(/:day))" => "info#about", :constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ }
match "/secret" => "info#about", :constraints => { :user_agent => /Firefox/ }
constraints :host => /localhost/ do
match "/secret" => "info#about"
end
match "/hello" => proc { |env| [200, {}, "Hello Rack!"] }
end
Detour::Application.routes.draw do |map| # map.resources :products, :member => { :detailed => :get } resources :products do get :detailed, :on => :member end # map.resources :forums, :collection => { :sortable => :get, :sort => :put } do |forums| # forums.resources :topics # end resources :forums do collection do get :sortable put :sort end resources :topics end # map.root :controller => "home", :action => "index" root :to => "home#index" # map.about "/about", :controller => "info", :action => "about" match "/about(.:format)" => "info#about", :as => :about match "/:year(/:month(/:day))" => "info#about", :constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ } match "/secret" => "info#about", :constraints => { :user_agent => /Firefox/ } constraints :host => /localhost/ do match "/secret" => "info#about" end match "/hello" => proc { |env| [200, {}, "Hello Rack!"] } end
info/about.html.erb
<%= debug params %>
<%= debug params %>
