Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 Finding the right :id in a controller using a specific route
    primarykey
    data
    text
    <p>I have my routes arranged so that when visiting the site the <code>:id</code> is displayed before the slug like so</p> <pre><code>match "/causes/:id/:slug" =&gt; "causes#show", :as =&gt; :cause, :via =&gt; 'get' </code></pre> <p>But I also have a nested attribute called "post" that belongs to causes like so</p> <pre><code>match "/causes/:id/:slug/posts" =&gt; "causes#posts", :via =&gt; 'get', :as =&gt; :posts </code></pre> <p>When I use this, everything works great for the causes, but not for the posts.</p> <p>If I use </p> <pre><code>@post = Post.find(params[:id]) </code></pre> <p>in causes or posts controller it always looks for the ID of the causes, and not the :id of the posts. So if the post <code>:id</code> is 9, and the cause <code>:id</code> is 1, and I use </p> <pre><code>@post = Post.find(params[:id]) </code></pre> <p>it will always look for <code>post[1]</code> and not 9 or whatever the post id really is.</p> <p>What am I doing wrong? Is there a way to make this work in the routes, or maybe a different way to find the id of a nested object in the controller?</p> <p>I need the route to be the way I have it set up, <code>:id/:slug...</code></p> <p>rake routes information:</p> <pre><code> cause GET /causes/:id/:slug(.:format) causes#show edit_cause GET /causes/:id/:slug/edit(.:format) causes#edit PUT /causes/:id/:slug(.:format) causes#update posts GET /causes/:id/:slug/posts(.:format) causes#posts POST /causes/:id/:slug/posts(.:format) PUT /causes/:id/:slug/posts(.:format) causes#update_post DELETE /causes/:id/:slug/posts(.:format) causes#destroy_post causes GET /causes(.:format) causes#index POST /causes(.:format) causes#create </code></pre> <p>Any help would be great.</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.
 

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