<?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::PolymorphicRoutes</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::PolymorphicRoutes</td> </tr> <tr class="top-aligned-row"> <td><strong>In:</strong></td> <td> <a href="../../files/lib/action_controller/polymorphic_routes_rb.html"> lib/action_controller/polymorphic_routes.rb </a> <br /> </td> </tr> </table> </div> <!-- banner header --> <div id="bodyContent"> <div id="contextContent"> <div id="description"> <p> Polymorphic URL helpers are methods for smart resolution to a named route call when given an Active Record model instance. They are to be used in combination with <a href="Resources.html">ActionController::Resources</a>. </p> <p> These methods are useful when you want to generate correct URL or path to a RESTful resource without having to know the exact type of the record in question. </p> <p> Nested resources and/or namespaces are also supported, as illustrated in the example: </p> <pre> polymorphic_url([:admin, @article, @comment]) </pre> <p> results in: </p> <pre> admin_article_comment_url(@article, @comment) </pre> <h2>Usage within the framework</h2> <p> Polymorphic URL helpers are used in a number of places throughout the Rails framework: </p> <ul> <li><tt>url_for</tt>, so you can use it with a record as the argument, e.g. <tt>url_for(@article)</tt>; </li> <li><a href="../ActionView/Helpers/FormHelper.html">ActionView::Helpers::FormHelper</a> uses <tt><a href="PolymorphicRoutes.html#M000124">polymorphic_path</a></tt>, so you can write <tt>form_for(@article)</tt> without having to specify <tt>:url</tt> parameter for the form action; </li> <li><tt>redirect_to</tt> (which, in fact, uses <tt>url_for</tt>) so you can write <tt>redirect_to(post)</tt> in your controllers; </li> <li><a href="../ActionView/Helpers/AtomFeedHelper.html">ActionView::Helpers::AtomFeedHelper</a>, so you don‘t have to explicitly specify URLs for feed entries. </li> </ul> <h2>Prefixed polymorphic helpers</h2> <p> In addition to <tt><a href="PolymorphicRoutes.html#M000123">polymorphic_url</a></tt> and <tt><a href="PolymorphicRoutes.html#M000124">polymorphic_path</a></tt> methods, a number of prefixed helpers are available as a shorthand to <tt>:action => "…"</tt> in options. Those are: </p> <ul> <li><tt>edit_polymorphic_url</tt>, <tt>edit_polymorphic_path</tt> </li> <li><tt>new_polymorphic_url</tt>, <tt>new_polymorphic_path</tt> </li> </ul> <p> Example usage: </p> <pre> edit_polymorphic_path(@post) # => "/posts/1/edit" polymorphic_path(@post, :format => :pdf) # => "/posts/1.pdf" </pre> </div> </div> <div id="method-list"> <h3 class="section-bar">Methods</h3> <div class="name-list"> <a href="#M000126">formatted_polymorphic_path</a> <a href="#M000125">formatted_polymorphic_url</a> <a href="#M000124">polymorphic_path</a> <a href="#M000123">polymorphic_url</a> </div> </div> </div> <!-- if includes --> <div id="section"> <!-- if method_list --> <div id="methods"> <h3 class="section-bar">Public Instance methods</h3> <div id="method-M000126" class="method-detail"> <a name="M000126"></a> <div class="method-heading"> <a href="PolymorphicRoutes.src/M000126.html" target="Code" class="method-signature" onclick="popupCode('PolymorphicRoutes.src/M000126.html');return false;"> <span class="method-name">formatted_polymorphic_path</span><span class="method-args">(record_or_hash, options = {})</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000125" class="method-detail"> <a name="M000125"></a> <div class="method-heading"> <a href="PolymorphicRoutes.src/M000125.html" target="Code" class="method-signature" onclick="popupCode('PolymorphicRoutes.src/M000125.html');return false;"> <span class="method-name">formatted_polymorphic_url</span><span class="method-args">(record_or_hash, options = {})</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000124" class="method-detail"> <a name="M000124"></a> <div class="method-heading"> <a href="PolymorphicRoutes.src/M000124.html" target="Code" class="method-signature" onclick="popupCode('PolymorphicRoutes.src/M000124.html');return false;"> <span class="method-name">polymorphic_path</span><span class="method-args">(record_or_hash_or_array, options = {})</span> </a> </div> <div class="method-description"> <p> Returns the path component of a URL for the given record. It uses <tt><a href="PolymorphicRoutes.html#M000123">polymorphic_url</a></tt> with <tt>:routing_type => :path</tt>. </p> </div> </div> <div id="method-M000123" class="method-detail"> <a name="M000123"></a> <div class="method-heading"> <a href="PolymorphicRoutes.src/M000123.html" target="Code" class="method-signature" onclick="popupCode('PolymorphicRoutes.src/M000123.html');return false;"> <span class="method-name">polymorphic_url</span><span class="method-args">(record_or_hash_or_array, options = {})</span> </a> </div> <div class="method-description"> <p> Constructs a call to a named RESTful route for the given record and returns the resulting URL string. For example: </p> <pre> # calls post_url(post) polymorphic_url(post) # => "http://example.com/posts/1" polymorphic_url([blog, post]) # => "http://example.com/blogs/1/posts/1" polymorphic_url([:admin, blog, post]) # => "http://example.com/admin/blogs/1/posts/1" polymorphic_url([user, :blog, post]) # => "http://example.com/users/1/blog/posts/1" </pre> <h4>Options</h4> <ul> <li><tt>:action</tt> - Specifies the action prefix for the named route: <tt>:new</tt> or <tt>:edit</tt>. Default is no prefix. </li> <li><tt>:routing_type</tt> - Allowed values are <tt>:path</tt> or <tt>:url</tt>. Default is <tt>:url</tt>. </li> </ul> <h4>Examples</h4> <pre> # an Article record polymorphic_url(record) # same as article_url(record) # a Comment record polymorphic_url(record) # same as comment_url(record) # it recognizes new records and maps to the collection record = Comment.new polymorphic_url(record) # same as comments_url() </pre> </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>