Reflecting on Rails Routing
Examining Routes in the Application Console
Dumping Routes
jruby script/console
rs = ActionController::Routing::Routes
puts rs.routes # will give you a chart of all defined routes. Each route includes a request method, pattern and params.
puts rs.named_routes.each {|name, route| printf(“%-30s %s\n”, name, r)}; nil
Anatomy of a Route Object
Look at a YAML (Yet Another Markup Language) representation of this route. You can also check out routing.rb and resources.rb in the ActionController source tree. You will see Routing, RouteSet and more.
Recognition and Generation in the Console
jruby ./script/console
>> irb ActionController::Routing::Routes # giving the irb command inside IRB sets self to the route set.
# To see a route generated from parameters…
generate(:controller => ‘bids’, :auction_id => 3, :action => ‘create’ ) # => /auctions/3/bids
generate(:controller => ‘bids’, :user_id => 3, :id => 4, :action => ‘retract’ ) #=> /users/3/bids/4/retract
generate(:controller => ‘bids’, :user_id => 3, :action => ‘manage’ ) #=> /users/3/bids/manage
generate(:controller => ‘auctions’, :action => ‘history’, :id => 3 ) #=> /history/3
# If item_year requires a year parameter to consist of 4 digits…
generate(:controller => ‘items’, :action => ‘item_year’, :year => 1939) #=> /item_year/1939
generate(:controller => ‘items’, :action => ‘item_year’, :year => 19393 ) #=> /items/item_year?year=19393
# You can go the other direction as well.
recognize_path(“/”) #=> {:controller => ‘auctions’, :action => ‘index’}
recognize_path(“/auctions”, :method => :get) # => {:controller => ‘auctions’, :action => ‘index’}
recognize_path(“/auctions”, :method => :post) # => {:controller => ‘auctions’, :action => ‘create’}
recognize_path(“/auctions/3/bids”, :method => :post) #=> {:controller => ‘bids’, :action =>’create’, :auction_id=>”3″}
recognize_path(“/auctions/3/bids/1/retract”, :method => :get) # => {:controller => ‘bids’, :action => ‘retract’, :auction_id=>”3″, :id=>1}
recognize_path(“/history/3″) #=> {:controller => ‘auctions’, :action => history, :id=>”3″}
recognize_path(“/item_year/1939″) #=>{:controller=>’items’, :action=>’item_year’, :year=>”1939″}
recognize_path(“/item_year/19393″) #=>ActionController::RoutingError: no route found to match /item_year/19393
Named Routes in the Console
You can also try out named routes in the console.
include ActionController::UrlWriter #=> Object
default_url_options[:host] = “example.com” #=> example.com
auction_url(1) #=> http://example.com/auctions/1
formatted_auction_url(1,”xml”) #=> http://example.com/auctions/1.xml
formatted_auctions_url(“xml”) #=> http://example.com/auctions.xml
Testing Routes
You can write tests that use the following methods: assert_generates, assert_recognizes, and assert_routing (which is the first two combined into one). You can make good use of these testing assertions along with RSpec.
Routing Navigator Plugin
Rick Olson has created this plugin to get an uber-version of the console for routing information in your browser.
# To install
sudo ./script/plugin install http://svn.techno-weenie.net/projects/plugins/routing_navigator/
# To use, tell a controller that you want them to show you the routing information you are interested in.
# In a controller definition….
routing_navigator
n # don’t leave this on in production.

Leave a Reply