我正在尝试创建一个事件应用,其中每个事件都有多个表,每个表有多个人坐在一张桌子上,事件有多个票据,将人们映射到他们所在的表格 - >为了实现这一点,我创建了一个嵌套在fields_for:tables中的复选框(在事件形式中依次)我认为强参数或表单本身有问题,但我无法找到任何信息这提供了问题的解决方案。检查表单中的复选框,指示哪些人将坐在此表并提交表单并返回表单,我发现复选框不再被检查???
这是我的模型文件的内容
# models
class Event < ActiveRecord::Base
has_many :tables, dependent: :destroy
has_many :people , through: :tickets
has_many :tickets
accepts_nested_attributes_for :tickets, allow_destroy: true
accepts_nested_attributes_for :tables, allow_destroy: true
end
class Table < ActiveRecord::Base
belongs_to :event
has_many :tickets
has_many :people, through: :tickets
end
class Ticket < ActiveRecord::Base
belongs_to :table
belongs_to :person
end
class Person < ActiveRecord::Base
has_many :tickets
has_many :tables, through: :tickets
end
为简洁起见,这里省略了部分表格。
<%= form_for(@event) do |f| %>
...
<%= f.fields_for :tables do |builder| %>
<%= render 'table_field', f: builder %>
<% end %>
<%= link_to_add_fields "Add Table", f, :tables %>
...
<% end %>
这是我在table_field
中实施的复选框列表。
<% Person.all.each do |person| %>
<div class="field">
<%= check_box_tag "table[people_ids][]", person.id, f.object.people.include?(person) %> <%= f.label [person.first_name, person.last_name].join(" ") %>
</div>
<% end %>
这是event_params
def event_params
params.require(:event).permit(:name, :description, :start, :end, :latitude, :longitude, :address, :data, :people_ids => [], tables_attributes: [:id, :number, :size, :people_ids => []]).tap do |whitelisted|
whitelisted[:data] = params[:event][:data]
end
如何在此表单中持续检查复选框?
答案 0 :(得分:0)
您可以使用http://apidock.com/rails/v4.0.2/ActionView/Helpers/FormOptionsHelper/collection_check_boxes
<%= f.collection_check_boxes(:people_ids, Person.all, :id, :name) do |person| %>
<%= person.label { person.check_box } %>
<% end %>
它也会保留数据。