Topic: Nested form won't work after moving fields to partial

I basically followed the Tutorials
http://railscasts.com/episodes/73-complex-forms-part-1 and
http://railscasts.com/episodes/74-complex-forms-part-2
and got stuck whenever it comes to the point to put the fields for the child class in a seperate partial to enable them to be dynamically added and removed using RJS.
The Error I get:

NameError in People#new
Showing app/views/people/_contactform.html.erb where line #2 raised:
undefined local variable or method `contact' for #<ActionView::Base:0x4b89eb8>

Here are the files:
class Person < ActiveRecord::Base
    has_many :contacts
        has_many :types, :through => :contacts

def contact_attributes=(contact_attributes)
  contact_attributes.each do |attributes|
    contacts.build(attributes)
  end
end


class Contact < ActiveRecord::Base
belongs_to :type
    belongs_to :person
end

Also the controler is set to build the contacts in the new function of people.

views/people/new.html.erb

<% form_for(@person) do |f| %>

   
  <%= f.error_messages %>

  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.label :givenname %><br />
    <%= f.text_field :givenname %>
  </p>
  <p>
    <%= f.label :gender %><br />
    <%= f.text_field :gender %>
  </p>
.
.
.

<div id="contacts">
<%= render :partial => 'contactform', :collection => @person.contacts %>
</div>

.
.
.

    <%= f.submit 'Create' %>
  </p>
<% end %>


views/people/_contactform.html.erb

<div class="contacts">
         <% fields_for "person[contact_attributes][]", contact do |contact_form| %>
  <p>
    Type:<%= contact_form.text_field :type_id %>
    Remark:<%= contact_form.text_field :remark %>
    Entry: <%= contact_form.text_field :entry  %>

        <%= link_to_function "remove", "this.up('.contacts').remove()"   %>
  </p>
        <% end %>
</div>


Before moving the last part to a partial an having it in new.html.erb everything worked fine. But in order to be able to add several contacts, the fields_for are supposed to be in that partial... what's wrong with the
<% fields_for ... , contact ... %>

Any suggestions?

Re: Nested form won't work after moving fields to partial

I wish I could help you, but I'm having an identical problem. Does it help to know you're not alone?

~Firefly

Re: Nested form won't work after moving fields to partial

Well it's good to know :-) Let's hope another one had the problem and actually solved it...

Re: Nested form won't work after moving fields to partial

As always. You're sitting over one of these problems a whole day and a couple of hours after posting it in a forum you find your solution. And here it is:

views/people/new.html.erb

<div id="contacts">
<%= render :partial => 'contactform', :object => @person.contacts %>
</div>
   <%= link_to_function "Add a contact" do |page|
     page.insert_html :bottom, :contacts, :partial => 'contactform', :object => Contact.new
     end %>

views/people/_contactform.html.erb
  <% for contact in @person.contacts do %>
         <% fields_for "person[contact_attributes][]", contact do |contact_form| %>
<div class="contacts">
<p>
    Type:<%= contact_form.collection_select( 'type_id', Type.all, :id, :name ) %>
    Remark:<%= contact_form.text_field :remark %>
    Entry: <%= contact_form.text_field :entry  %>

        <%= link_to_function "remove", "this.up('.contacts').remove()"   %>
  </p>
        <% end %>
</div>
        <% end %>


Not nice, but working. To make the Script-Thing work, don't forget to include the defauls javascript links in layouts/people.html.erb

Re: Nested form won't work after moving fields to partial

I tried this and it looked great--until I clicked "submit." At that point it said that "contact_attributes" was an unknown attribute.

I'm going to sleep on it.
A big part of my problem is that I'm such a newb that I don't really understand what is going on, and I'm probably missing something obvious.

Thanks for sharing your code, though. I feel closer to a solution.

Re: Nested form won't work after moving fields to partial

I have the exact same problem...and I don't have a solution for it either. It's odd... apparently others can get the same bit of code to work.