title: Rails & JQuery: Invalid association. Make sure that accepts_nested_attributes_for is used for
body: <p>Although I'd like to add <code>link_to_add</code> with using <code>simple_nested_form_for</code>, the following error was displayed.</p>
  
  <p><code>ArgumentError (Invalid association. Make sure that accepts_nested_attributes_for is used for :events association.):</code></p>
  
  <p>There are similar questions in stackoverflow, but it doesn't work for me. So I post this as a new question.</p>
  
  <p>The error was appeared when I add <code>f.link_to_add</code> in <code>_schedule_form.html.erb</code>.</p>
  
  <p><strong>_schedule_form.html.erb</strong></p>
  
  <pre><code>&lt;%= f.label :title %&gt;
  &lt;%= f.text_field :title, class: 'form-control' %&gt;
  &lt;br&gt;
  &lt;%= f.label :departure_date %&gt;
  &lt;div class="input-group date" id="datetimepicker"&gt;
    &lt;%= f.text_field :departure_date, class: 'form-control' %&gt;
    &lt;span class="input-group-addon"&gt;
      &lt;span class="glyphicon glyphicon-calendar"&gt;&lt;/span&gt;
    &lt;/span&gt;
  &lt;/div&gt;
  &lt;script type="text/javascript"&gt;
    $(function () {
      $('#datetimepicker').datetimepicker({format:'MMM-DD-YYYY'});
    });
  &lt;/script&gt;
  &lt;br&gt;
  &lt;div id="room"&gt;
    &lt;%= f.simple_fields_for :rooms do |a| %&gt;
      &lt;p class="day-number-element-selector"&gt;&lt;b&gt;Day&amp;nbsp;&lt;%= a.index.to_i + 1 %&gt;&lt;/b&gt;&lt;/p&gt;
      &lt;div id="event"&gt;
        &lt;% a.simple_fields_for :events do |e| %&gt;
          &lt;%= e.input :from %&gt;
        &lt;% end %&gt;
      &lt;/div&gt;
  
      #add here!!!
  
      &lt;%= f.link_to_add "Add event", :events, data: {target: '#event'}, class: "btn btn-primary" %&gt;
  
      &lt;%= a.input :room %&gt;
  
    &lt;% end %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>new_html.erb</strong></p>
  
  <pre><code>&lt;div class="row"&gt;
    &lt;div class="col-md-12"&gt;
      &lt;p&gt;Create schedule&lt;/p&gt;
      &lt;%= simple_nested_form_for(@schedule) do |f| %&gt;
        &lt;%= render 'schedule_form', f: f %&gt;
        &lt;%= f.submit "Create my schedule", class: "btn btn-primary" %&gt;
        &lt;br&gt;
      &lt;% end %&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Give the following models:</p>
  
  <pre><code>class Schedule &lt; ActiveRecord::Base
    belongs_to :user
    has_many :rooms
    accepts_nested_attributes_for :rooms, allow_destroy: true
    ...
  
  class Room &lt; ActiveRecord::Base
    belongs_to :schedule
    has_many :events
    accepts_nested_attributes_for :events, allow_destroy: true
    ...
  
  class Event &lt; ActiveRecord::Base
    belongs_to :room
    ...
  </code></pre>
  
  <p><strong>schedule_controller.rb</strong></p>
  
  <pre><code>class SchedulesController &lt; ApplicationController
  ...
    before_action :set_schedule,  only: %i(show edit update destroy)
  ...
    def new
      @schedule = Schedule.new
      room = @schedule.rooms.build
      room.events.build
    end
  
    def create
      @schedule = current_user.schedules.build(schedule_params)
      if @schedule.save
        flash[:success] = "schedule created!"
        redirect_to root_url
      else
        render 'new'
      end
    end
  
    def edit
      @day_max = Room.where("schedule_id = ?", @schedule.id).maximum(:day)
    end
  
    def update
      @schedule.rooms.maximum(:day)
      if @schedule.update(schedule_params)
        flash[:success] = "schedule updated!"
        redirect_to root_url
      else
        render 'edit'
      end
    end
  
    private
  
      def schedule_params
        params.require(:schedule).permit(:title, :departure_date, rooms_attributes: [:id, :_destroy, :room, :day, events_attributes: [:id, :_destroy, :from, :to, :title, :detail]])
      end
  
      def set_schedule
        @schedule = Schedule.find(params[:id])
      end
  </code></pre>
  
  <p>No error have been displayed before adding <code>link_to_add</code>.</p>
  
  <p>It would be appreciated if you could give me any suggestion.</p>
  
  <p><strong>SOLVED!!!</strong></p>
  
  <pre><code>  &lt;div id="room"&gt;
      &lt;%= f.simple_fields_for :rooms do |a| %&gt;
        &lt;div id="room_&lt;%= a.object.object_id %&gt;"&gt;
          &lt;p class="day-number-element-selector"&gt;&lt;b&gt;Day&amp;nbsp;&lt;%= a.index.to_i + 1 %&gt;&lt;/b&gt;&lt;/p&gt;
  
          &lt;%= a.simple_fields_for :events do |e| %&gt;
            &lt;span class="form-inline"&gt;
              &lt;%= e.input :from, label: false %&gt;&amp;nbsp;&amp;nbsp;
              &lt;%= e.input :to, label: false%&gt;
            &lt;/span&gt;
              &lt;%= e.input :title, label: 'event' %&gt;
          &lt;% end %&gt;
        &lt;/div&gt;
  
        &lt;%= a.link_to_add "Add event", :events, data: {target: "#room_#{a.object.object_id}"}, class: "btn btn-primary" %&gt;
  
        &lt;%= a.input :room %&gt;
  
      &lt;% end %&gt;
    &lt;/div&gt;
  </code></pre>