雖然我想使用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 <%= 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 <%= a.index.to_i + 1 %></b></p>
<%= a.simple_fields_for :events do |e| %>
<span class="form-inline">
<%= e.input :from, label: false %>
<%= 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>
試試這個:'<%= a.link_to_add 「添加事件」:事件, data:{target:'#event'},class:「btn btn-primary」%>' –
感謝您的快速回復,@ Muhammad Yawar Ali。當我創建新數據時,沒有顯示錯誤並且保存了「事件」。但是當我嘗試編輯數據時,事件數據不會顯示。如果你有任何想法,將不勝感激。 – SamuraiBlue