<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>Ruby on Rails Notes &#187; routes</title> <atom:link href="http://railsnotes.com/tag/routes/feed/" rel="self" type="application/rss+xml" /><link>http://railsnotes.com</link> <description>A code-heavy brain dump of stuff I come across working on Ruby on Rails projects including Models, ActiveRecord, Views, Controllers, RESTful rails, deployment, server stuff, etc.</description> <lastBuildDate>Sun, 29 Aug 2010 23:45:16 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <item><title>Understanding Rails Routes</title><link>http://railsnotes.com/213-understanding-routes-rb/</link> <comments>http://railsnotes.com/213-understanding-routes-rb/#comments</comments> <pubDate>Tue, 21 Apr 2009 20:39:39 +0000</pubDate> <dc:creator>Melvin Ram</dc:creator> <category><![CDATA[Other]]></category> <category><![CDATA[basics]]></category> <category><![CDATA[routes]]></category><guid
isPermaLink="false">http://www.railsnotes.com/?p=213</guid> <description><![CDATA[The config/routes.rb file is an important piece of your app. In this post, I&#8217;ll go through the standard file that is generated when you create a new Rails project. Let&#8217;s go through each section one at a time. # The priority is based upon order of creation: first created -&#62; highest priority. At this comment suggests, [...]]]></description> <content:encoded><![CDATA[<p></p><p>The config/routes.rb file is an important piece of your app. In this post, I&#8217;ll go through the standard file that is generated when you create a new Rails project. Let&#8217;s go through each section one at a time.</p><pre># The priority is based upon order of creation: first created -&gt; highest priority.</pre><p>At this comment suggests, routes specified higher up in the file have highest priority. This means if you have two routes that handle the same incoming request, the first one will work and the second one will be ignored.</p><h2>Structure of a route</h2><pre>map.connect 'products/:id', :controller =&gt; 'catalog', :action =&gt; 'view'</pre><p>With this route, if someone visits /products/1, Rails will call the &#8216;view&#8217; action in the &#8216;catalog&#8217; controller and will pass it the value of 1 as params[:id]. Notice the structure of this route:</p><pre>map.SOMETHING URL_TO_HANDLE, WHERE_TO_ROUTE_THIS_REQUEST</pre><p>As we go through more examples, pay attention to these groups.  Now one thing the above route won&#8217;t do for you is make it easy to reference this url from a link_to or from a form_for. That&#8217;s what the named routes are for.</p><h2>Named Routes</h2><pre>map.purchase 'products/:id/purchase', :controller =&gt; 'catalog', :action =&gt; 'purchase'</pre><p>Named routes allow you to reference the route throughout your app. For example, you could put this inside a view</p><pre>&lt;%= link_to "Buy Now", purchase_url(:id =&gt; product.id) %&gt;</pre><p>and it would output a link that would be handled by the above route:</p><pre>&lt;a href="http://domain/products/12/purchase"&gt;Buy Now&lt;/a&gt;</pre><p>You could also use it inside a controller to redirect to this page with:</p><pre>:redirect_to purchase_url(:id =&gt; @product.id)</pre><p>Get the idea? Good. Let&#8217;s move on to resources.</p><h2>Resources in Routes.rb</h2><pre>map.resources :products</pre><p>The above one line of code gives you a bunch of named routes:</p><pre>= GET routes
1. products_path         /products
2. product_path(1)       /product
3. new_product_path      /products/new
4. edit_product_path(1)  /products/1/edit</pre><pre>= POST Routes
5. products_path(:method =&gt; :post)    /products
6. product_path(1, :method =&gt; :post)  /products/1</pre><pre>= DESTROY Route
7. product_path(1, :method =&gt; :destroy)  /products/1</pre><p>These 7 routes allow you to tell your application what it should do (create, read, update or delete records) using just HTTP requests. And you get all that with one line in your routes.rb. Cool, huh? For more info, google &#8220;<a
href="http://www.google.com/search?q=restful+rails">restful rails</a>&#8220;.</p><h2>:member &amp; :collection routes for Resources</h2><p>Okay great. But what if you want to do things that isn&#8217;t included by default in a resource? That&#8217;s what member &amp; collections options are for.</p><pre>map.resources :products, :member =&gt; { :short =&gt; :get, :toggle =&gt; :post }, :collection =&gt; { :sold =&gt; :get }</pre><p>:member &amp; :collection allow you to add additional named routes to your resources. Let&#8217;s start with :collection.  Collection adds routes to the entire collection. If you had a resource of Contacts, a collection route you might add would be delete_all_older_than_6_months and the route to get to it would be be /contacts/delete_all_older_than_6_months/.  A member method applies only to one record. For example, you might add a member method called upgrade_to_vip that would update a specific contact to have a status of VIP. And the route would be /contacts/1/upgrade_to_vip.  Even though Rails makes it easy to add member &amp; collection routes to your resource, you should think twice before adding one. Why? Because most likely, you can achieve whatever your trying to do with just the 7 default routes.  Alright, now lets move to nested resources.</p><h2>Routes for Nested Resources</h2><pre>map.resources :products, :has_many =&gt; [ :comments, :sales ], :has_one =&gt; :seller</pre><p>The above nested resources allow you to do:</p><pre>new_product_comment_path(1)   /products/1/comments/new
product_comment_path(1, 1)    /products/1/comments/1
product_seller_path(1)        /products/1/seller</pre><p>The :has_many &amp; :has_one options are great for simple nested resources, but what if you need to add more options to the resources that are nested? In that case, instead of using :has_many &amp; :has_one, you&#8217;ll use a block.</p><pre>map.resources :products do |products|
  products.resources :comments
  products.resources :sales, :collection =&gt; { :recent =&gt; :get }
end</pre><p>This will allow you to add :member &amp; :collection routes as well as other specifics to each nested resource.</p><h2>Namespaces in Routes</h2><pre>map.namespace :admin do |admin|
  admin.resources :products
end

admin_products_path # goes to /admin/products</pre><p>Let&#8217;s say you have an ecommerce web app. You might want the admin view of the products page to be different than the normal view. Route namespaces allow you to do that.</p><pre># Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)</pre><p>As the comment indicates, requests on resources within a namespace don&#8217;t get directed to ProductsController what would normally be located at app/controllers/products_controller.rb.  Instead, it directs the request to Admin::ProductsController which needs to located at app/controllers/admin/products_controller.rb.  Why couldn&#8217;t you just use nested resources? Because admin is not a resource (it doesn&#8217;t have a table inside the database).</p><h2>Root Route</h2><pre>map.root :controller =&gt; "welcome"</pre><p>map.root is a special route that handles http requests for &#8216;/&#8217;. It&#8217;s used to specify what you want to show as your home page.</p><h2>Rake Routes</h2><pre># See how all your routes lay out with "rake routes"</pre><p>If you run &#8220;rake routes&#8221;, you&#8217;ll see a list of all your routes with columns for: route name, HTTP method, route path &amp; route requirements. It&#8217;s a good way to do a sanity check that your routes were properly setup.</p><h2>Default Routes</h2><pre># Install the default routes as the lowest priority.
# Note: These default routes make all actions in every controller accessible via GET requests. You should
# consider removing the them or commenting them out if you're using named routes and resources.
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'</pre><p>The default routes are there to catch routes that haven&#8217;t been specified. It&#8217;ll try to guess the controller &amp; action based on the URL that is requested. It&#8217;s much less useful now that resources have become the dominant way of doing things in Rails.</p> ]]></content:encoded> <wfw:commentRss>http://railsnotes.com/213-understanding-routes-rb/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Simple Routing Gotcha</title><link>http://railsnotes.com/50-simple-routing-gotcha/</link> <comments>http://railsnotes.com/50-simple-routing-gotcha/#comments</comments> <pubDate>Thu, 02 Oct 2008 18:15:59 +0000</pubDate> <dc:creator>Melvin Ram</dc:creator> <category><![CDATA[routes]]></category> <category><![CDATA[gotcha]]></category><guid
isPermaLink="false">http://railsnotes.wordpress.com/?p=50</guid> <description><![CDATA[Rails will always look at the public folder and serve that if it exists. If someone goes to yourdomain.com/monkey.html, it will look first at the public folder to see if there is a file that matches. If not, it will look for a route that matches. If no matching route exists, Rails will raise an [...]]]></description> <content:encoded><![CDATA[<p></p><p>Rails will always look at the public folder and serve that if it exists. If someone goes to yourdomain.com/monkey.html, it will look first at the public folder to see if there is a file that matches. If not, it will look for a route that matches. If no matching route exists, Rails will raise an exception (throw an error.)</p><p>Here&#8217;s the conversion that prompted this post:</p><blockquote><p>glennfu: I have this route in my routes.rb file: map.pages &#8216;/*path&#8217;, :controller =&gt; &#8220;pages&#8221;, :action =&gt; &#8220;show&#8221;</p><p>glennfu: but it&#8217;s catching too much&#8230; like my favicon and some stylesheets.  How can I make it smarter?</p><p><span
style="color:#008000">melvinram: Are they (the favicon &amp; stylesheets) in the public folder?</span></p><p>glennfu: yes</p><p><span
style="color:#008000">melvinram: Rails should look at the public folder first&#8230;</span></p><p>glennfu: perhaps that&#8217;s a clue, I&#8217;ll make sure they&#8217;re actually on the server&#8230;</p><p>glennfu: you spotted the problem, they&#8217;re actually not in the public folder!</p><p>glennfu: those files are missing</p></blockquote> ]]></content:encoded> <wfw:commentRss>http://railsnotes.com/50-simple-routing-gotcha/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Collection &amp; Member Methods within a Route</title><link>http://railsnotes.com/5-collection-member-methods-within-a-route/</link> <comments>http://railsnotes.com/5-collection-member-methods-within-a-route/#comments</comments> <pubDate>Fri, 29 Aug 2008 09:46:27 +0000</pubDate> <dc:creator>Melvin Ram</dc:creator> <category><![CDATA[routes]]></category><guid
isPermaLink="false">http://railsnotes.wordpress.com/?p=5</guid> <description><![CDATA[What&#8217;s the difference between collection &#38; member methods inside a Rails route? This conversation should help you understand: RubyHunt: What&#8217;s the use of the :collection and :member methods within a route? What&#8217;s the difference between the two? melvinram: Collection adds routes to the entire collection RubyHunt: So the collection is used to add another action [...]]]></description> <content:encoded><![CDATA[<p></p><p>What&#8217;s the difference between collection &amp; member methods inside a Rails route? This conversation should help you understand:</p><blockquote><p><strong>RubyHunt:</strong> What&#8217;s the use of the :collection and :member methods within a route? What&#8217;s the difference between the two?</p><p><strong><span
style="color:#008000">melvinram:</span></strong><span
style="color:#008000"> Collection adds routes to the entire collection</span></p><p><strong>RubyHunt:</strong> So the collection is used to add another action inside the controller</p><p><strong><span
style="color:#008000">melvinram:</span></strong><span
style="color:#008000"> If you had a resource of Contacts, a collection route you might add would be delete_all_older_than_6_months</span></p><p><strong><span
style="color:#008000">melvinram: </span></strong><span
style="color:#008000">and the route to get to it would be be </span><span
style="font-style:italic"><span
style="color:#008000">/contacts/delete_all_older_than_6_months/</span></span></p><p><strong>RubyHunt: </strong>Which otherwise would be restricted to only 7 actions ?</p><p><strong><span
style="color:#008000">melvinram: </span></strong><span
style="color:#008000">yes</span></p><p><span
style="color:#008000"><span
style="color:#008000"><strong>melvinram</strong></span><span
style="color:#008000">: </span><span
style="color:#008000">A member method applies only to one record. For example, you might add a member method called upgrade_to_vip that would update one record to have a status of VIP or something&#8230;</span></span></p><p><strong><span
style="color:#008000">melvinram: </span></strong><span
style="color:#008000">And the route would be /contacts/1/upgrade_to_vip</span></p><p><strong>RubyHunt:</strong> So the :collection is used to add action inside a RESTful controller and the :member would be used to apply action for individual object.</p><p><strong><span
style="color:#008000">melvinram:</span></strong><span
style="color:#008000"> you got it!</span></p></blockquote> ]]></content:encoded> <wfw:commentRss>http://railsnotes.com/5-collection-member-methods-within-a-route/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (user agent is rejected)
Database Caching 4/16 queries in 0.039 seconds using disk

Served from: railsnotes.com @ 2010-09-10 05:47:06 -->