1

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 %>
         <!-- # 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
         -->
         <%= subitem.food.name %>
         <%= subitem.food.calories %>

         <%= 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. As a result, an update fails:

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

EDIT: 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:

<%= subitem.food.name %>
<%= subitem.food.calories %>

Getting this information won't work if I am using a form builder with partials, at least, not in my trials.

Edit 2:* See the edit in the code. Here's my MealLineItem

class MealLineItem < ActiveRecord::Base
  # Associations ---------------------
  belongs_to :food
  belongs_to :meal
end

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:

meal_line_item.food.name
MunkiPhD
  • 3,636
  • 1
  • 29
  • 51

3 Answers3

3

Is there f. missing from <%= fields_for?

--edit

Have you tried:

<%= f.fields_for 'meal[meal_line_item][]', subitem do |line_item_form| %>

--edit

Docs say that it should work without loop too:

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

  <%= f.fields_for :meal_line_items 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 %>

  <%= f.submit %>
<% end %>
Heikki
  • 15,329
  • 2
  • 54
  • 49
  • I missed copying the f. when copying the code over from trying different things :( – MunkiPhD Jan 05 '11 at 12:30
  • Nope. For reference, without the f. the inputs essentially don't state that they're part of a 'Meal' and ends up with the following id: meal_line_item_#_servings – MunkiPhD Jan 05 '11 at 12:47
  • That throws a "`@meal[meal[meal_line_item]' is not allowed as an instance variable name" – MunkiPhD Jan 05 '11 at 15:22
  • That WILL work, but the thing is it won't solve the problem I'm having. I must have mistyped my initial problem to begin with. The thing is I need to display information that is based on an association on the meal_line_item for those that are already existing. See edit 2. Thanks for the help so far! – MunkiPhD Jan 05 '11 at 16:06
1

Have to test this but maybe this approach?

_form

<%= form.fields_for :meal_line_items do |meal_line_item_form| %>
 <% @meal.meal_line_items.each do |meal_line_item| %>
  <%= render :partial => "meal_line_items/meal_line_item", :locals => { :meal_line_item_form => meal_line_item_form, :meal_line_item => meal_line_item } %>
 <% end %>
<% end %>

meal_line_items/_meal_line_item.erb

<%= meal_line_item_form.label :servings %><br/>
<%= meal_line_item_form.text_field :servings %><br/>
<%= meal_line_item_form.label :food_id %><br/>
<%= meal_line_item_form.text_field :food_id %><br/>

EDIT

here's a link to an example for setting the formbuilder iterator directly (Rails 2.3.8 though). The associations between Outbreak -> Incidents -> Location should be similiar to the ones for Meal -> Meal_line_items -> Food.

AJAX update of accepts_nested_attributes_for partials

Community
  • 1
  • 1
Pasted
  • 864
  • 1
  • 12
  • 22
  • If I do as you stated, I get a NameError when I attempt to use information from meal_line_item in the partial saying that there's an undefined local variable, even though the name of the local being passed in is the same as it is in the partial. – MunkiPhD Jan 05 '11 at 15:42
  • The models have this sort of association? sorry about the formatting... Meal has_many: meal_line_items accepts_nested_attributes_for :meal_line_item :::::: Meal_line_item belongs_to: meal – Pasted Jan 05 '11 at 15:58
  • Yes, but meal_line_item also belongs_to food. So when I want to look at the food item through the association on that meal line item, I cant get the thing to work in the partial. Maybe I'm passing the variables incorrectly, but the code you posted didn't work for me with the locals, nor any other method I tried. – MunkiPhD Jan 05 '11 at 16:01
  • Added a link to a page on setting the Formbuilder iterator directly, although there's alot of padding there. Probably overkill unless you needed to add meal_line_items via ajax calls (and even then there must be a better way of doing this). – Pasted Jan 05 '11 at 16:18
  • It's not so much adding the items via ajax - that works correctly. It's displaying items that already exist. The code you originally posted of doing the fields_for and then the loop inside, essentially displays the code twice as it's a double for loop. – MunkiPhD Jan 05 '11 at 16:52
0

After searching high and low, I found the error. Although I was modifying the partial and was receiving a NameError it's because I was calling the partial from a helper method - exactly the same problem as stated in the following question:

rails fields_for render partial with multiple locals producing undefined variable

Community
  • 1
  • 1
MunkiPhD
  • 3,636
  • 1
  • 29
  • 51