<?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; basics</title> <atom:link href="http://railsnotes.com/tag/basics/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>[Link] Ruby Basics</title><link>http://railsnotes.com/284-link-ruby-basics/</link> <comments>http://railsnotes.com/284-link-ruby-basics/#comments</comments> <pubDate>Fri, 24 Apr 2009 20:50:36 +0000</pubDate> <dc:creator>Melvin Ram</dc:creator> <category><![CDATA[Other]]></category> <category><![CDATA[basics]]></category> <category><![CDATA[ruby]]></category><guid
isPermaLink="false">http://www.railsnotes.com/?p=284</guid> <description><![CDATA[Peter Cooper posted a new blog entry that links to 17 Videos Covering Basic Ruby Techniques. These videos cover the basics and may not have any surprising new info but it&#8217;s nice to review the basics from time to time. Take a look.]]></description> <content:encoded><![CDATA[<p></p><p>Peter Cooper posted a new blog entry that links to <a
href="http://www.rubyinside.com/17-videos-covering-basic-ruby-techiques-1685.html">17 Videos Covering Basic Ruby Techniques</a>. These videos cover the basics and may not have any surprising new info but it&#8217;s nice to review the basics from time to time. Take a look.</p> ]]></content:encoded> <wfw:commentRss>http://railsnotes.com/284-link-ruby-basics/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <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> </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 2/16 queries in 0.021 seconds using disk

Served from: railsnotes.com @ 2010-09-09 07:22:11 -->