<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Module: ActionController::Routing</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" /> <script type="text/javascript"> // <![CDATA[ function popupCode( url ) { window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400") } function toggleCode( id ) { if ( document.getElementById ) elem = document.getElementById( id ); else if ( document.all ) elem = eval( "document.all." + id ); else return false; elemStyle = elem.style; if ( elemStyle.display != "block" ) { elemStyle.display = "block" } else { elemStyle.display = "none" } return true; } // Make codeblocks hidden by default document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" ) // ]]> </script> </head> <body> <div id="classHeader"> <table class="header-table"> <tr class="top-aligned-row"> <td><strong>Module</strong></td> <td class="class-name-in-header">ActionController::Routing</td> </tr> <tr class="top-aligned-row"> <td><strong>In:</strong></td> <td> <a href="../../files/lib/action_controller/routing/builder_rb.html"> lib/action_controller/routing/builder.rb </a> <br /> <a href="../../files/lib/action_controller/routing/route_set_rb.html"> lib/action_controller/routing/route_set.rb </a> <br /> <a href="../../files/lib/action_controller/routing/optimisations_rb.html"> lib/action_controller/routing/optimisations.rb </a> <br /> <a href="../../files/lib/action_controller/routing/recognition_optimisation_rb.html"> lib/action_controller/routing/recognition_optimisation.rb </a> <br /> <a href="../../files/lib/action_controller/routing/segments_rb.html"> lib/action_controller/routing/segments.rb </a> <br /> <a href="../../files/lib/action_controller/routing/route_rb.html"> lib/action_controller/routing/route.rb </a> <br /> <a href="../../files/lib/action_controller/routing_rb.html"> lib/action_controller/routing.rb </a> <br /> </td> </tr> </table> </div> <!-- banner header --> <div id="bodyContent"> <div id="contextContent"> <div id="description"> <h2><a href="Routing.html">Routing</a></h2> <p> The routing module provides URL rewriting in native Ruby. It‘s a way to redirect incoming requests to controllers and actions. This replaces mod_rewrite rules. Best of all, Rails’ <a href="Routing.html">Routing</a> works with any web server. Routes are defined in <tt>config/routes.rb</tt>. </p> <p> Consider the following route, installed by Rails when you generate your application: </p> <pre> map.connect ':controller/:action/:id' </pre> <p> This route states that it expects requests to consist of a <tt>:controller</tt> followed by an <tt>:action</tt> that in turn is fed some <tt>:id</tt>. </p> <p> Suppose you get an incoming request for <tt>/blog/edit/22</tt>, you‘ll end up with: </p> <pre> params = { :controller => 'blog', :action => 'edit', :id => '22' } </pre> <p> Think of creating routes as drawing a map for your requests. The map tells them where to go based on some predefined pattern: </p> <pre> ActionController::Routing::Routes.draw do |map| Pattern 1 tells some request to go to one place Pattern 2 tell them to go to another ... end </pre> <p> The following symbols are special: </p> <pre> :controller maps to your controller name :action maps to an action with your controllers </pre> <p> Other names simply map to a parameter as in the case of <tt>:id</tt>. </p> <h2>Route priority</h2> <p> Not all routes are created equally. Routes have priority defined by the order of appearance of the routes in the <tt>config/routes.rb</tt> file. The priority goes from top to bottom. The last route in that file is at the lowest priority and will be applied last. If no route matches, 404 is returned. </p> <p> Within blocks, the empty pattern is at the highest priority. In practice this works out nicely: </p> <pre> ActionController::Routing::Routes.draw do |map| map.with_options :controller => 'blog' do |blog| blog.show '', :action => 'list' end map.connect ':controller/:action/:view' end </pre> <p> In this case, invoking blog controller (with an URL like ’/blog/’) without parameters will activate the ‘list’ action by default. </p> <h2>Defaults routes and default parameters</h2> <p> Setting a default route is straightforward in Rails - you simply append a Hash at the end of your mapping to set any default parameters. </p> <p> Example: </p> <pre> ActionController::Routing:Routes.draw do |map| map.connect ':controller/:action/:id', :controller => 'blog' end </pre> <p> This sets up <tt>blog</tt> as the default controller if no other is specified. This means visiting ’/’ would invoke the blog controller. </p> <p> More formally, you can include arbitrary parameters in the route, thus: </p> <pre> map.connect ':controller/:action/:id', :action => 'show', :page => 'Dashboard' </pre> <p> This will pass the :page parameter to all incoming requests that match this route. </p> <p> Note: The default routes, as provided by the Rails generator, make all actions in every controller accessible via GET requests. You should consider removing them or commenting them out if you‘re using named routes and resources. </p> <h2>Named routes</h2> <p> Routes can be named with the syntax <tt>map.name_of_route options</tt>, allowing for easy reference within your source as <tt>name_of_route_url</tt> for the full URL and <tt>name_of_route_path</tt> for the URI path. </p> <p> Example: </p> <pre> # In routes.rb map.login 'login', :controller => 'accounts', :action => 'login' # With render, redirect_to, tests, etc. redirect_to login_url </pre> <p> Arguments can be passed as well. </p> <pre> redirect_to show_item_path(:id => 25) </pre> <p> Use <tt>map.root</tt> as a shorthand to name a route for the root path "". </p> <pre> # In routes.rb map.root :controller => 'blogs' # would recognize http://www.example.com/ as params = { :controller => 'blogs', :action => 'index' } # and provide these named routes root_url # => 'http://www.example.com/' root_path # => '' </pre> <p> You can also specify an already-defined named route in your <tt>map.root</tt> call: </p> <pre> # In routes.rb map.new_session :controller => 'sessions', :action => 'new' map.root :new_session </pre> <p> Note: when using <tt>with_options</tt>, the route is simply named after the method you call on the block parameter rather than map. </p> <pre> # In routes.rb map.with_options :controller => 'blog' do |blog| blog.show '', :action => 'list' blog.delete 'delete/:id', :action => 'delete', blog.edit 'edit/:id', :action => 'edit' end # provides named routes for show, delete, and edit link_to @article.title, show_path(:id => @article.id) </pre> <h2>Pretty URLs</h2> <p> Routes can generate pretty URLs. For example: </p> <pre> map.connect 'articles/:year/:month/:day', :controller => 'articles', :action => 'find_by_date', :year => /\d{4}/, :month => /\d{1,2}/, :day => /\d{1,2}/ </pre> <p> Using the route above, the URL "<a href="http://localhost:3000/articles/2005/11/06">localhost:3000/articles/2005/11/06</a>" maps to </p> <pre> params = {:year => '2005', :month => '11', :day => '06'} </pre> <h2>Regular Expressions and parameters</h2> <p> You can specify a regular expression to define a format for a parameter. </p> <pre> map.geocode 'geocode/:postalcode', :controller => 'geocode', :action => 'show', :postalcode => /\d{5}(-\d{4})?/ </pre> <p> or, more formally: </p> <pre> map.geocode 'geocode/:postalcode', :controller => 'geocode', :action => 'show', :requirements => { :postalcode => /\d{5}(-\d{4})?/ } </pre> <p> Formats can include the ‘ignorecase’ and ‘extended syntax’ regular expression modifiers: </p> <pre> map.geocode 'geocode/:postalcode', :controller => 'geocode', :action => 'show', :postalcode => /hx\d\d\s\d[a-z]{2}/i map.geocode 'geocode/:postalcode', :controller => 'geocode', :action => 'show',:requirements => { :postalcode => /# Postcode format \d{5} #Prefix (-\d{4})? #Suffix /x } </pre> <p> Using the multiline match modifier will raise an ArgumentError. Encoding regular expression modifiers are silently ignored. The match will always use the default encoding or ASCII. </p> <h2>Route globbing</h2> <p> Specifying <tt>*[string]</tt> as part of a rule like: </p> <pre> map.connect '*path' , :controller => 'blog' , :action => 'unrecognized?' </pre> <p> will glob all remaining parts of the route that were not recognized earlier. The globbed values are in <tt>params[:path]</tt> as an array of path segments. </p> <h2>Route conditions</h2> <p> With conditions you can define restrictions on routes. Currently the only valid condition is <tt>:method</tt>. </p> <ul> <li><tt>:method</tt> - Allows you to specify which method can access the route. Possible values are <tt>:post</tt>, <tt>:get</tt>, <tt>:put</tt>, <tt>:delete</tt> and <tt>:any</tt>. The default value is <tt>:any</tt>, <tt>:any</tt> means that any method can access the route. </li> </ul> <p> Example: </p> <pre> map.connect 'post/:id', :controller => 'posts', :action => 'show', :conditions => { :method => :get } map.connect 'post/:id', :controller => 'posts', :action => 'create_comment', :conditions => { :method => :post } </pre> <p> Now, if you POST to <tt>/posts/:id</tt>, it will route to the <tt>create_comment</tt> action. A GET on the same URL will route to the <tt>show</tt> action. </p> <h2>Reloading routes</h2> <p> You can reload routes if you feel you must: </p> <pre> ActionController::Routing::Routes.reload </pre> <p> This will clear all named routes and reload routes.rb if the file has been modified from last load. To absolutely force reloading, use <tt>reload!</tt>. </p> <h2>Testing Routes</h2> <p> The two main methods for testing your routes: </p> <h3><tt>assert_routing</tt></h3> <pre> def test_movie_route_properly_splits opts = {:controller => "plugin", :action => "checkout", :id => "2"} assert_routing "plugin/checkout/2", opts end </pre> <p> <tt>assert_routing</tt> lets you test whether or not the route properly resolves into options. </p> <h3><tt>assert_recognizes</tt></h3> <pre> def test_route_has_options opts = {:controller => "plugin", :action => "show", :id => "12"} assert_recognizes opts, "/plugins/show/12" end </pre> <p> Note the subtle difference between the two: <tt>assert_routing</tt> tests that a URL fits options while <tt>assert_recognizes</tt> tests that a URL breaks into parameters properly. </p> <p> In tests you can simply pass the URL or named route to <tt>get</tt> or <tt>post</tt>. </p> <pre> def send_to_jail get '/jail' assert_response :success assert_template "jail/front" end def goes_to_login get login_url #... end </pre> <h2>View a list of all your routes</h2> <p> Run <tt>rake routes</tt>. </p> </div> </div> <div id="method-list"> <h3 class="section-bar">Methods</h3> <div class="name-list"> <a href="#M000187">controller_relative_to</a> <a href="#M000188">inflections_with_route_reloading</a> <a href="#M000184">normalize_paths</a> <a href="#M000185">possible_controllers</a> <a href="#M000186">use_controllers!</a> <a href="#M000183">with_controllers</a> </div> </div> </div> <!-- if includes --> <div id="section"> <div id="class-list"> <h3 class="section-bar">Classes and Modules</h3> Module <a href="Routing/Helpers.html" class="link">ActionController::Routing::Helpers</a><br /> Module <a href="Routing/Optimisation.html" class="link">ActionController::Routing::Optimisation</a><br /> Class <a href="Routing/OptionalFormatSegment.html" class="link">ActionController::Routing::OptionalFormatSegment</a><br /> </div> <div id="constants-list"> <h3 class="section-bar">Constants</h3> <div class="name-list"> <table summary="Constants"> <tr class="top-aligned-row context-row"> <td class="context-item-name">SEPARATORS</td> <td>=</td> <td class="context-item-value">%w( / . ? )</td> </tr> <tr class="top-aligned-row context-row"> <td class="context-item-name">HTTP_METHODS</td> <td>=</td> <td class="context-item-value">[:get, :head, :post, :put, :delete, :options]</td> </tr> <tr class="top-aligned-row context-row"> <td class="context-item-name">ALLOWED_REQUIREMENTS_FOR_OPTIMISATION</td> <td>=</td> <td class="context-item-value">[:controller, :action].to_set</td> </tr> <tr class="top-aligned-row context-row"> <td class="context-item-name">Routes</td> <td>=</td> <td class="context-item-value">RouteSet.new</td> </tr> </table> </div> </div> <!-- if method_list --> <div id="methods"> <h3 class="section-bar">Public Class methods</h3> <div id="method-M000187" class="method-detail"> <a name="M000187"></a> <div class="method-heading"> <a href="Routing.src/M000187.html" target="Code" class="method-signature" onclick="popupCode('Routing.src/M000187.html');return false;"> <span class="method-name">controller_relative_to</span><span class="method-args">(controller, previous)</span> </a> </div> <div class="method-description"> <p> Returns a controller path for a new <tt>controller</tt> based on a <tt>previous</tt> controller path. Handles 4 scenarios: </p> <ul> <li>stay in the previous controller: <pre> controller_relative_to( nil, "groups/discussion" ) # => "groups/discussion" </pre> </li> <li>stay in the previous namespace: <pre> controller_relative_to( "posts", "groups/discussion" ) # => "groups/posts" </pre> </li> <li>forced move to the root namespace: <pre> controller_relative_to( "/posts", "groups/discussion" ) # => "posts" </pre> </li> <li>previous namespace is root: <pre> controller_relative_to( "posts", "anything_with_no_slashes" ) # =>"posts" </pre> </li> </ul> </div> </div> <div id="method-M000184" class="method-detail"> <a name="M000184"></a> <div class="method-heading"> <a href="Routing.src/M000184.html" target="Code" class="method-signature" onclick="popupCode('Routing.src/M000184.html');return false;"> <span class="method-name">normalize_paths</span><span class="method-args">(paths)</span> </a> </div> <div class="method-description"> <p> Returns an array of paths, cleaned of double-slashes and relative path references. </p> <ul> <li>"\\" and "//" become "\" or "/". </li> <li>"/foo/bar/../config" becomes "/foo/config". </li> </ul> <p> The returned array is sorted by length, descending. </p> </div> </div> <div id="method-M000185" class="method-detail"> <a name="M000185"></a> <div class="method-heading"> <a href="Routing.src/M000185.html" target="Code" class="method-signature" onclick="popupCode('Routing.src/M000185.html');return false;"> <span class="method-name">possible_controllers</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns the array of controller names currently available to <a href="Routing.html">ActionController::Routing</a>. </p> </div> </div> <div id="method-M000186" class="method-detail"> <a name="M000186"></a> <div class="method-heading"> <a href="Routing.src/M000186.html" target="Code" class="method-signature" onclick="popupCode('Routing.src/M000186.html');return false;"> <span class="method-name">use_controllers!</span><span class="method-args">(controller_names)</span> </a> </div> <div class="method-description"> <p> Replaces the internal list of controllers available to <a href="Routing.html">ActionController::Routing</a> with the passed argument. </p> <pre> ActionController::Routing.use_controllers!([ "posts", "comments", "admin/comments" ]) </pre> </div> </div> <div id="method-M000183" class="method-detail"> <a name="M000183"></a> <div class="method-heading"> <a href="Routing.src/M000183.html" target="Code" class="method-signature" onclick="popupCode('Routing.src/M000183.html');return false;"> <span class="method-name">with_controllers</span><span class="method-args">(names) {|| ...}</span> </a> </div> <div class="method-description"> <p> Expects an array of controller names as the first argument. Executes the passed block with only the named controllers named available. This method is used in internal Rails testing. </p> </div> </div> <h3 class="section-bar">Public Instance methods</h3> <div id="method-M000188" class="method-detail"> <a name="M000188"></a> <div class="method-heading"> <a href="Routing.src/M000188.html" target="Code" class="method-signature" onclick="popupCode('Routing.src/M000188.html');return false;"> <span class="method-name">inflections_with_route_reloading</span><span class="method-args">(&block)</span> </a> </div> <div class="method-description"> <p> Ensures that routes are reloaded when Rails inflections are updated. </p> </div> </div> </div> </div> <div id="validator-badges"> <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p> </div> </body> </html>