title: Newbie stuck with fields_for and nested attributes in rails
body: <p>Rails newbie here. I'm stuck on fields_for in Rails, and would be eternally grateful for anyone's help.</p>
  
  <ul>
  <li>In Events#new, why am I getting NoMethodError? 
  Error message says undefined method `name' </li>
  <li>In Events, why am I getting NameError?
  Error message says undefined local variable or method 'event'. </li>
  </ul>
  
  <p>I am trying to create and view a form where each event has 2 fields for each friend.</p>
  
  <p>_form.html.erb:</p>
  
  <pre><code>&lt;%= form_for(@event) do |f| %&gt;
   &lt;div class="field"&gt;
  Date: &lt;%= f.text_field :date %&gt;&lt;/br&gt;
  Friends: &lt;%= f.fields_for :friends do |friends_fields| %&gt;
    &lt;%= friends_fields.text_field :name %&gt;
    &lt;% end %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>index.html.erb:</p>
  
  <pre><code>&lt;tbody&gt;
  &lt;% @events.each do |event_form| %&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;%= event_form.date %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= event_form.friends.each do |friend| %&gt;
        &lt;%= friend.name %&gt;
        &lt;% end %&gt;
      &lt;/td&gt;
      &lt;td&gt;&lt;%= link_to 'Show', event %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= link_to 'Edit', edit_event_path(event) %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= link_to 'Destroy', event, method: :delete, data: { confirm: 'Are you sure?' } %&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p></p>
  
  <p>event.rb:</p>
  
  <pre><code>class Event &lt; ActiveRecord::Base
      has_many :friends
      accepts_nested_attributes_for :friends, allow_destroy: true
  end
  </code></pre>
  
  <p>friend.rb:</p>
  
  <pre><code>class Friend &lt; ActiveRecord::Base
      belongs_to :event
  end
  </code></pre>
  
  <p>events_controller.rb:</p>
  
  <pre><code>def new
      @event = Event.new
      2.times { @event.friends.build }
  end
  </code></pre>