Help to render partial from fieds_for with nested attributes

I’m using nested_attributes and trying to implement the add/remove
fields on-the-fly throu ajax following Ryan B. screencast about
Nested Model (#196)

Doing this, it works fine:

<%= f.fields_for :item_parts do |parts_form| %>
  <p class="fields">
    <%= parts_form.label :part_id %>
    <%= parts_form.select :part_id, Part.all.collect { |x|

[ x.title, x.id ]} %>

<%= parts_form.hidden_field :_destroy %>
<%= link_to_remove_fields “remove”, parts_form %>


<% end %>

But when trying to pass the form fields to a partial like this, it
returns the following error:

undefined method `part_id’ for #Part:0x00000103c4fed8

Extracted source (around line #3):

1: <p class="fields">
2:   <%= f.label :part_id %>
3:   <%= f.select :part_id, Part.all.collect { |x| [ x.title,

x.id ]} %>

4: <%= f.hidden_field :_destroy %>
5: <%= link_to_remove_fields “remove”, f %>
6:

The relevant code for the form calls the partial:

<%= f.fields_for :item_parts do |parts_form| %>
  <%= render 'part_fields', :f => parts_form %>
<% end %>

partial: /part_fields.html.erb

<p class="fields">
  <%= f.label :part_id %>
  <%= f.select :part_id, Part.all.collect { |x| [ x.title, x.id ]}

%>

<%= f.hidden_field :_destroy %>
<%= link_to_remove_fields “remove”, f %>

So, any help on what I am doing wrong at this point?

A friend of mine just realize that the problem is related to the
javascript add function.

And now I’m trying to fix it.

<%= add_child_link "Add Part", f, :item %>

on my Item model I hold the accepts_nested_attributes_for :item_parts

but I still getting a error

undefined method `klass’ for nil:NilClass

this error refers to the object variable on this:

def add_child_link(name, form_builder, association)
object =
form_builder.object.class.reflect_on_association(association).klass.new

end

any help?

On Aug 4, 2011, at 4:24 PM, Kleber S. wrote:

A friend of mine just realize that the problem is related to the
javascript add function.

And now I’m trying to fix it.

<%= add_child_link "Add Part", f, :item %>

Look outside of this line. Are you sure you are inside of the
form_for @your_model do |f| loop at the moment you see this line?
The error sounds like you’re outside of it, and thus builder.object
(inside the helper) doesn’t resolve to @ your_model.

Walter

hi Walter,

thank you for replying.

it’s inside the form_for code block

On Aug 4, 2011, at 11:25 PM, Kleber S. wrote:

hi Walter,

thank you for replying.

it’s inside the form_for code block

Either engage the debugger or put a simple puts statement inside your
view helper, and see what exactly builder is pointing to at that
point. If it’s nil, then step up a level, into the view partial, and
see what f is set to just before it is passed to the helper. Lather,
rinse, repeat, until you figure out where this property is being
dropped. If other aspects of your page are working, and just this one
thing is not, then you’ve obviously re-assigned the variable at some
point (my money is on a typo). I just built a site following this
Railscast last month, and it went perfectly.

Walter