Note that there are some explanatory texts on larger screens.

plurals
  1. POBest restfull way to create a child item from parent item?
    primarykey
    data
    text
    <p>As a newbie, I have been searching around for THE best practice to create a child item when displaying a parent item. I found many explanations, rather incomplete and not evident to me. Can some one describe the complete implementation of the best practice ? I created <code>parent_item</code> and <code>child_item</code> models includind <code>has_many</code> and <code>belongs_to</code> declarations. I can list linked <code>child_items</code> (manually created) in the <code>parent_item</code> "show" view.</p> <p>Now I'd like to create a <code>&lt;%= link_to 'New Child Item', new_child_item_path %&gt;</code> passing parent's id as a parameter in the URL. Which way to go now ?</p> <ul> <li>modify the routes ?</li> <li>add a parameter to <code>child_item.new</code> controller ?</li> <li>have a hidden field in the child_item form to store the <code>parent_item.id</code> value ?</li> </ul> <p>I'll be glad to give a hug to the best explanation !</p> <p>Thanks !</p> <p>Fred</p> <hr> <p>Hi, thanks for your help. As I'm discovering this nesting resources functionality, I tried to apply it with success : when creating the child from the parent, the form is displayed with the parent_id that has been passed to it.</p> <p>Here are my code updates : (playground is the parent, landscape is the child)</p> <p><strong>ROUTES</strong></p> <pre><code>resources :playgrounds do resources :landscapes, :only=&gt;[:new, :create] end </code></pre> <p><strong>SHOW</strong> (from playground parent)</p> <pre><code> &lt;%= link_to 'Add landscape', new_playground_landscape_path(@playground) %&gt; </code></pre> <p><strong>LANDSCAPE_CONTROLLER</strong></p> <pre><code>def new @landscape = Landscape.new @landscape.playground_id = params[:playground_id] </code></pre> <p><strong>FORM</strong> (from landscape) - I did not need to refer to @playground as I only have landscape fields in this form (Am I right ?)</p> <pre><code>&lt;%= form_for [@landscape] do |f| %&gt; ... &lt;div class="field"&gt; &lt;%= f.label :playground_id %&gt;&lt;br /&gt; &lt;%=f.text_field :playground_id%&gt; &lt;/div&gt; </code></pre> <p>Great, it works fine. But the playground_id field should not appear in the form, and be defined as not accessible attribute in the landscape.rb model. Are these requirements compatible ? Shall I hide the field, and leave it accessible ?</p> <p>Thanks for your help !</p> <p>Fred</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. 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