Note that there are some explanatory texts on larger screens.

plurals
  1. PORails Nested Attributes Doesn't Insert ID Correctly
    primarykey
    data
    text
    <p>I'm attempting to edit a model's nested attributes, much as outline <a href="http://guides.rubyonrails.org/form_helpers.html" rel="nofollow">here</a>, replicated here:</p> <pre><code> &lt;%= form_for @person do |person_form| %&gt; &lt;%= person_form.text_field :name %&gt; &lt;% for address in @person.addresses %&gt; &lt;%= person_form.fields_for address, :index =&gt; address do |address_form|%&gt; &lt;%= address_form.text_field :city %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>In my code, I have the following:</p> <pre><code>&lt;%= form_for(@meal) do |f| %&gt; &lt;!-- some other stuff that's irrelevant... --&gt; &lt;% for subitem in @meal.meal_line_items %&gt; &lt;!-- # Edit 2: I need to display information here about the subitem Which I can't find a way to pass it to the partial, or work in this manner for existing items --&gt; &lt;%= subitem.food.name %&gt; &lt;%= subitem.food.calories %&gt; &lt;%= f.fields_for subitem, :index =&gt; subitem do |line_item_form| %&gt; &lt;%= line_item_form.label :servings %&gt;&lt;br/&gt; &lt;%= line_item_form.text_field :servings %&gt;&lt;br/&gt; &lt;%= line_item_form.label :food_id %&gt;&lt;br/&gt; &lt;%= line_item_form.text_field :food_id %&gt;&lt;br/&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;%= f.submit %&gt; &lt;% end %&gt; </code></pre> <p>This works great, except, when I look at the HTML, it's creating the inputs that look like the following, failing to input the correct id and instead placing the memory representation(?) of the model. As a result, an update fails:</p> <pre><code>&lt;input type="text" value="2" size="30" name="meal[meal_line_item][#&lt;MealLineItem:0x00000005c5d618&gt;][servings]" id="meal_meal_line_item_#&lt;MealLineItem:0x00000005c5d618&gt;_servings"&gt; </code></pre> <p><strong>EDIT:</strong> The reason I'm attempting to do it in this method is that I need to gather some information on associations for existing meal_line_items. For example, in the area where I took out code, I have some code to the effect of:</p> <pre><code>&lt;%= subitem.food.name %&gt; &lt;%= subitem.food.calories %&gt; </code></pre> <p>Getting this information won't work if I am using a form builder with partials, at least, not in my trials.</p> <p><strong>Edit 2:*</strong> See the edit in the code. Here's my MealLineItem</p> <pre><code>class MealLineItem &lt; ActiveRecord::Base # Associations --------------------- belongs_to :food belongs_to :meal end </code></pre> <p>And meal accepts_nested_attributes for the model. As you can see it belongs to both food and meal model. For the existing meal_line_item I need to do something like:</p> <pre><code>meal_line_item.food.name </code></pre>
    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