【问题标题】:Rails & JQuery: Invalid association. Make sure that accepts_nested_attributes_for is used forRails & JQuery:无效的关联。确保accepts_nested_attributes_for 用于
【发布时间】:2016-03-31 10:56:41
【问题描述】:

虽然我想使用simple_nested_form_for添加link_to_add,但显示以下错误。

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

stackoverflow 中有类似的问题,但对我不起作用。所以我将此作为一个新问题发布。

当我在_schedule_form.html.erb中添加f.link_to_add时出现错误。

_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>

给出以下模型:

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

添加link_to_add之前没有显示错误。

如果您能给我任何建议,我们将不胜感激。

已解决!!!

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

【问题讨论】:

  • 试试这个:&lt;%= a.link_to_add "Add event", :events, data: {target: '#event'}, class: "btn btn-primary" %&gt;
  • 感谢您的快速回复,@Muhammad Yawar Ali。当我创建新数据时,没有显示错误并且保存了event。但是当我尝试编辑数据时没有显示事件数据。如果您有任何想法,将不胜感激。

标签: javascript jquery ruby-on-rails ruby ruby-on-rails-4


【解决方案1】:

div#room 应该是这样的:

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

【讨论】:

  • 感谢您的回答,@Muhammad Yawar Ali。尽管我尝试了您的代码,但当我单击 link_to_add 时它没有响应。如果您能给我其他建议,我们将不胜感激。
  • 你试过f而不是a&lt;%= f.link_to_add "Add event", :events, data: {target: '#event'}, class: "btn btn-primary" %&gt;
  • 感谢您的评论,@Muhammad Yawar Ali。虽然我试过f而不是a,但Invalid association. Make sure that accepts_nested_attributes_for is used for :events association.又被显示出来了。
  • 我接受你的回答,@Muhammad,因为我的第一个问题Invalid association 已解决。谢谢。
  • 您是否解决了上述错误或一直困扰您的问题?
【解决方案2】:

首先nested_form gem 被废弃了,所以我建议使用cocoon,它现在是最新的

也可能你的属性被过滤了

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

因为你的房间属性应该是复数形式 rooms_attributes

最后一个建议不要使用多重嵌套。尝试查看表单对象模式

希望对你有帮助

【讨论】:

  • 感谢您的回答,@Sergey。我不知道nested_form gem 被遗弃了。感谢您的信息。虽然我稍后会尝试使用 cocoon,但如果您能给我任何想法,将不胜感激。因为我把room_attributes修改成了rooms_attributes,结果是一样的。
  • 正如 Muhammad Yawar Ali 在他的回答中指出的那样,您似乎在错误的 form_object 上调用了 link_to_add。你在 f 上调用它时应该在 a 上调用它
猜你喜欢
  • 2014-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多