0

Although I'd like to add link_to_add with using simple_nested_form_for, the following error was displayed.

ArgumentError (Invalid association. Make sure that accepts_nested_attributes_for is used for :events association.):

There are similar questions in stackoverflow, but it doesn't work for me. So I post this as a new question.

The error was appeared when I add f.link_to_add in _schedule_form.html.erb.

_schedule_form.html.erb

<%= f.label :title %>
<%= f.text_field :title, class: 'form-control' %>
<br>
<%= f.label :departure_date %>
<div class="input-group date" id="datetimepicker">
  <%= f.text_field :departure_date, class: 'form-control' %>
  <span class="input-group-addon">
    <span class="glyphicon glyphicon-calendar"></span>
  </span>
</div>
<script type="text/javascript">
  $(function () {
    $('#datetimepicker').datetimepicker({format:'MMM-DD-YYYY'});
  });
</script>
<br>
<div id="room">
  <%= f.simple_fields_for :rooms do |a| %>
    <p class="day-number-element-selector"><b>Day&nbsp;<%= a.index.to_i + 1 %></b></p>
    <div id="event">
      <% a.simple_fields_for :events do |e| %>
        <%= e.input :from %>
      <% end %>
    </div>

    #add here!!!

    <%= f.link_to_add "Add event", :events, data: {target: '#event'}, class: "btn btn-primary" %>

    <%= a.input :room %>

  <% end %>
</div>

new_html.erb

<div class="row">
  <div class="col-md-12">
    <p>Create schedule</p>
    <%= simple_nested_form_for(@schedule) do |f| %>
      <%= render 'schedule_form', f: f %>
      <%= f.submit "Create my schedule", class: "btn btn-primary" %>
      <br>
    <% end %>
  </div>
</div>

Give the following models:

class Schedule < ActiveRecord::Base
  belongs_to :user
  has_many :rooms
  accepts_nested_attributes_for :rooms, allow_destroy: true
  ...

class Room < ActiveRecord::Base
  belongs_to :schedule
  has_many :events
  accepts_nested_attributes_for :events, allow_destroy: true
  ...

class Event < ActiveRecord::Base
  belongs_to :room
  ...

schedule_controller.rb

class SchedulesController < 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

No error have been displayed before adding link_to_add.

It would be appreciated if you could give me any suggestion.

SOLVED!!!

  <div id="room">
    <%= f.simple_fields_for :rooms do |a| %>
      <div id="room_<%= a.object.object_id %>">
        <p class="day-number-element-selector"><b>Day&nbsp;<%= a.index.to_i + 1 %></b></p>

        <%= a.simple_fields_for :events do |e| %>
          <span class="form-inline">
            <%= e.input :from, label: false %>&nbsp;&nbsp;
            <%= e.input :to, label: false%>
          </span>
            <%= e.input :title, label: 'event' %>
        <% end %>
      </div>

      <%= a.link_to_add "Add event", :events, data: {target: "#room_#{a.object.object_id}"}, class: "btn btn-primary" %>

      <%= a.input :room %>

    <% end %>
  </div>
2
  • 1
    try this : <%= a.link_to_add "Add event", :events, data: {target: '#event'}, class: "btn btn-primary" %> Commented Mar 31, 2016 at 11:05
  • Thank you for your quick response, @ Muhammad Yawar Ali. No error was displayed and event was saved when I create new data. But the event data is not displayed when I try to edit the data. It would be appreciated if you could any idea. Commented Mar 31, 2016 at 11:47

2 Answers 2

1

div#room should be something like :

<div id="room">
  <%= f.simple_fields_for :rooms do |a| %>
    <p class="day-number-element-selector"><b>Day&nbsp;<%= a.index.to_i + 1 %></b></p>

      <% a.simple_fields_for :events do |e| %>
        <div id="event">
         <%= e.input :from %>
        </div>
      <% end %>
    #add here!!!

    <%= a.link_to_add "Add event", :events, data: {target: '#event'}, class: "btn btn-primary" %>

    <%= a.input :room %>

  <% end %>
</div>
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your answer, @Muhammad Yawar Ali. Although I tried your code, it was unresponsive when I click link_to_add. It would be appreciated if you could give me another suggestion.
Have you tried f instead of a <%= f.link_to_add "Add event", :events, data: {target: '#event'}, class: "btn btn-primary" %>
Thank you for your comment, @Muhammad Yawar Ali. Although I tried f instead of a, Invalid association. Make sure that accepts_nested_attributes_for is used for :events association. was displayed again.
I accept your answer, @Muhammad because my first question Invalid association was solved. Thank you.
Have you solved above error or its till bugging you ?
|
0

First of all nested_form gem is abandoned, so I would suggest to use cocoon which is up to date now

Also probably you attributes filtered

room_attributes: [..., events_attributes: [...]]

As your attributes for rooms should be in plural form rooms_attributes

And the last suggestion do not use multi nesting. Try to have a look on form objects pattern

Hope it helps

2 Comments

Thank you for your answer, @Sergey. I didn't know nested_form gem is abandoned. Thank you for your information. Although I will try to use cocoon later, it would be appreciated if you could give me any idea. Because I modified room_attributes to rooms_attributes, the result is the same.
as Muhammad Yawar Ali pointed out in his answer, it looks like you are calling link_to_add on the wrong form_object. you are calling it on f when it should be called on a

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.