2016-03-31 37 views
0

雖然我想使用simple_nested_form_for添加link_to_add,但顯示了以下錯誤。Rails&JQuery:無效關聯。確保使用的參數爲

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

有計算器中類似的問題,但它並沒有爲我工作。所以我把它作爲一個新問題發佈。

當我在_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> 
+1

試試這個:'<%= a.link_to_add 「添加事件」:事件, data:{target:'#event'},class:「btn btn-primary」%>' –

+0

感謝您的快速回復,@ Muhammad Yawar Ali。當我創建新數據時,沒有顯示錯誤並且保存了「事件」。但是當我嘗試編輯數據時,事件數據不會顯示。如果你有任何想法,將不勝感激。 – SamuraiBlue

回答

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

謝謝你的回答,@Muhammad Yawar Ali。雖然我嘗試了你的代碼,但是當我點擊'link_to_add'時,它沒有響應。如果你能給我另一個建議,我將不勝感激。 – SamuraiBlue

+0

你嘗試過'f'而不是'a'嗎?<%= f.link_to_add「添加事件」,:events,data:{target:'#event'},class:「btn btn-primary」%>' –

+0

感謝您的評論,@Muhammad Yawar Ali。儘管我嘗試了'f'而不是'a','無效的關聯。確保將accepts_nested_attributes_for用於:events association.'再次顯示。 – SamuraiBlue

0

所有nested_form寶石首先被放棄,所以我會建議使用cocoon這是最新的,現在

也可能是你的屬性過濾

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

至於你的屬性的房間應該是複數形式rooms_attributes

而最後的建議不使用多嵌套。嘗試看看在表單對象模式

希望它可以幫助

+0

謝謝你的回答,@Sergey。我不知道nested_form gem被放棄了。感謝您的信息。雖然以後我會嘗試使用繭,但是如果您能給我任何想法,我們將不勝感激。因爲我將'room_attributes'修改爲'rooms_attributes',結果是一樣的。 – SamuraiBlue

+0

正如Muhammad Yawar Ali在他的回答中指出的那樣,您看起來像是在錯誤的form_object上調用link_to_add。你應該在'a'上打電話給'f',並在'f'上呼叫它。 – mgidea