Rails Nested Attributes Doesn't Insert ID Correctly

Posted by MunkiPhD on Stack Overflow See other posts from Stack Overflow or by MunkiPhD
Published on 2011-01-05T01:29:55Z Indexed on 2011/01/05 14:53 UTC
Read the original article Hit count: 139

I'm attempting to edit a model's nested attributes, much as outline here, replicated here:

    <%= form_for @person do |person_form| %>
      <%= person_form.text_field :name %>
      <% for address in @person.addresses %>
        <%= person_form.fields_for address, :index => address do |address_form|%>
          <%= address_form.text_field :city %>
        <% end %>
      <% end %>
    <% end %>

In my code, I have the following:

<%= form_for(@meal) do |f| %>
  <!-- some other stuff that's irrelevant... -->

      <% for subitem in @meal.meal_line_items %>
         <%= f.fields_for subitem, :index => subitem do |line_item_form| %>
            <%= line_item_form.label :servings %><br/>
            <%= line_item_form.text_field :servings %><br/>
            <%= line_item_form.label :food_id %><br/>
            <%= line_item_form.text_field :food_id %><br/>
         <% end %>
      <% end %>

      <%= f.submit %>
<% end %>

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:

<input type="text" value="2" size="30" name="meal[meal_line_item][#<MealLineItem:0x00000005c5d618>][servings]" id="meal_meal_line_item_#<MealLineItem:0x00000005c5d618>_servings">

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about index