Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>They're both ways to add additional actions to a resource-based route in Rails.</p> <blockquote> <ul> <li>A member route requires an ID, because it acts on a member.</li> <li>A collection route doesn't require an ID because it acts on a collection of objects.</li> </ul> </blockquote> <p>I like to think of them in terms of RESTful URLs. Consider the basics for a resource/model <code>Foo</code></p> <pre><code>GET /foo # FooController#index GET /foo/:id # FooController#show GET /foo/new # FooController#new POST /foo # FooController#create GET /foo/:id/edit # FooController#edit PUT /foo/:id # FooController#update DELETE /foo/:id # FooController#destroy </code></pre> <p>Notice how:</p> <ul> <li>Some routes have :id placeholders for Foo.id, and so refer to a <em>specific</em> Foo</li> <li>Some routes have no :id, and thus refer to <em>all</em> Foos (and/or no specific foo, as in #new and #create)</li> <li>Some routes (index/create, show/update/destroy) have the same URL, and use HTTP methods to differentiate between them</li> <li>Some routes (edit/show) are basically the same (method &amp; URL prefix) except for a different suffix (including "no suffix") at the end.</li> </ul> <p>Member routes and collection routes let you add additional routes/actions using the same techniques as I listed above.</p> <p>A member route adds a custom action to a <em>specific instance</em> using the URL suffix and HTTP method you provide. So, if you had a member route declaration of <code>:member =&gt; { :bar =&gt; :get }</code>. you'd get an additional route of:</p> <pre><code>GET /foo/:id/bar # FooController#bar </code></pre> <p>Note how it overloads <code>GET /foo/:id</code> in the same way that `edit' does. This is how you'd implement a "delete" action that provides a UI for the "destroy" action.</p> <p>Similarly, a collection route adds an overload to the <em>collection</em> and/or a non-specific instance (it's up to you to decide exactly what it implies). So, if you declared <code>:collection =&gt; { :baz =&gt; :get }</code>, you'd get an additional route:</p> <pre><code>GET /foo/baz # FooController#baz </code></pre> <p>...in very much the same way as <code>new</code>.</p> <p>You can also customize the HTTP method.</p> <p>For example, I just recently had a project where I needed a "reply" action on a <code>Comment</code>. It's basically the same idea as <code>Comment#create</code> (which uses POST), except that it's in reference to a specific parent <code>Comment</code>. So, I created a member route: <code>:member =&gt; { :reply =&gt; :post }</code>. This gave me:</p> <pre><code>POST /comment/:id/reply # CommentController#reply </code></pre> <p>This keeps the routes restful while still expanding upon the basic 7 actions.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload