Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Travis R is correct. (I wish I could upvote ya.) I just got this working myself. With these routes:</p> <pre><code>resources :articles do resources :comments end </code></pre> <p>You get paths like:</p> <pre><code>/articles/42 /articles/42/comments/99 </code></pre> <p>routed to controllers at</p> <pre><code>app/controllers/articles_controller.rb app/controllers/comments_controller.rb </code></pre> <p>just as it says at <a href="http://guides.rubyonrails.org/routing.html#nested-resources" rel="noreferrer">http://guides.rubyonrails.org/routing.html#nested-resources</a>, with no special namespaces.</p> <p>But partials and forms become tricky. Note the square brackets:</p> <pre><code>&lt;%= form_for [@article, @comment] do |f| %&gt; </code></pre> <p>Most important, if you want a URI, you may need something like this:</p> <pre><code>article_comment_path(@article, @comment) </code></pre> <p>Alternatively:</p> <pre><code>[@article, @comment] </code></pre> <p>as described at <a href="http://edgeguides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects" rel="noreferrer">http://edgeguides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects</a></p> <p>For example, inside a collections partial with <code>comment_item</code> supplied for iteration,</p> <pre><code>&lt;%= link_to "delete", article_comment_path(@article, comment_item), :method =&gt; :delete, :confirm =&gt; "Really?" %&gt; </code></pre> <p>What jamuraa says may work in the context of Article, but it did not work for me in various other ways.</p> <p>There is a lot of discussion related to nested resources, e.g. <a href="http://weblog.jamisbuck.org/2007/2/5/nesting-resources" rel="noreferrer">http://weblog.jamisbuck.org/2007/2/5/nesting-resources</a></p> <p>Interestingly, I just learned that most people's unit-tests are not actually testing all paths. When people follow jamisbuck's suggestion, they end up with two ways to get at nested resources. Their unit-tests will generally get/post to the simplest:</p> <pre><code># POST /comments post :create, :comment =&gt; {:article_id=&gt;42, ...} </code></pre> <p>In order to test the route that they may prefer, they need to do it this way:</p> <pre><code># POST /articles/42/comments post :create, :article_id =&gt; 42, :comment =&gt; {...} </code></pre> <p>I learned this because my unit-tests started failing when I switched from this:</p> <pre><code>resources :comments resources :articles do resources :comments end </code></pre> <p>to this:</p> <pre><code>resources :comments, :only =&gt; [:destroy, :show, :edit, :update] resources :articles do resources :comments, :only =&gt; [:create, :index, :new] end </code></pre> <p>I guess it's ok to have duplicate routes, and to miss a few unit-tests. (Why test? Because even if the user never sees the duplicates, your forms may refer to them, either implicitly or via named routes.) Still, to minimize needless duplication, I recommend this:</p> <pre><code>resources :comments resources :articles do resources :comments, :only =&gt; [:create, :index, :new] end </code></pre> <p>Sorry for the long answer. Not many people are aware of the subtleties, I think.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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