Routing

Introduction

Anatomy of a Route


# In config/routes.rb
map.route_name 'url_pattern', params_hash
map.user_list 'users', :controller => 'users', :action => 'list'
map.root, :controller => 'home'

Route Defaults and Requirements


map.connect "blog/:year/:month/:day", 
      :controller => "blog", 
      :action => "show_date", 
      :requirements => { :year => /(19|20)\d\d/, 
        :month => /[01]?\d/, 
        :day => /[0-3]?\d/}, 
      :day => nil, 
      :month => nil

Using Named Routes


# In your views
link_to "User list", user_list_path # => <a href="/users">User List</a>
link_to "User list", user_list_url # => <a href="http://site.url/users">User List</a>
form_tag(search_path)
form_for(@person, :url => super_post_path)

# In a controller action
redirect_to root_path

url_for: Generating a URL from params


params = {"action"=>"show", "id"=>"2", "controller"=>"posts"}
url_for(params) #=> http://localhost:3000/posts/2
# url_for is used by link_to, redirect_to, form_for etc.

Wildcards


map.connect '*anything',
  :controller => 'blog',
  :action => 'unknown_request'
              
map.connect 'download/*path',
  :controller => 'file'
  :action => 'download'

Listing Routes with rake routes


# rake routes
# map.resources :posts
posts GET        /posts(.:format)               {:controller=>"posts", :action=>"index"}
      POST       /posts(.:format)               {:controller=>"posts", :action=>"create"}
new_post GET     /posts/new(.:format)           {:controller=>"posts", :action=>"new"}
edit_post GET    /posts/:id/edit(.:format)      {:controller=>"posts", :action=>"edit"}
     post GET    /posts/:id(.:format)           {:controller=>"posts", :action=>"show"}
          PUT    /posts/:id(.:format)           {:controller=>"posts", :action=>"update"}
          DELETE /posts/:id(.:format)           {:controller=>"posts", :action=>"destroy"}
# Default routes
/:controller/:action/:id           
/:controller/:action/:id(.:format)

Routes in the Console


rs = ActionController::Routing::Routes
puts rs.routes
rs.generate :controller => 'posts', :action => 'show', :id => 1
# => "/posts/1" 
rs.recognize_path("/posts/1", :method => :get)
#=> {:controller=>"posts", :action=>"show", :id=>"1"}

Testing Routes


def test_movie_route_properly_splits
    opts = {:controller => "plugin", :action => "checkout", :id => "2"}
    assert_routing "plugin/checkout/2", opts
end  
  
def test_route_has_options
  opts = {:controller => "plugin", :action => "show", :id => "12"}
  assert_recognizes opts, "/plugins/show/12" 
end