Rails中的嵌套复选框 [英] Nested checkboxes in Rails

查看:53
本文介绍了Rails中的嵌套复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个事件应用程序,其中每个事件有多个表,每个表有多个人坐在一张桌子上,该事件有多个票证,这些票证将人们映射到他们坐在的桌子上->为此,我创建了一个嵌套在fields_for:tables中的复选框(这又是事件表单),我认为强参数或表单本身有问题,但是我无法找到任何提供以下信息的信息:解决此问题的方法.在选中了指示哪些人将坐在这张桌子的表单中的复选框并提交表单并返回表单后,我发现不再选中该复选框了?

I'm trying to create an event app where each event has multiple tables and each table has multiple people sitting at a table the event has multiple tickets which map the people to the tables that they are sitting at -> in order to achieve this I have created a checkbox nested in the fields_for :tables (which is in turn in the event form) I presume something is wrong with either the strong parameters or the form itself but I have not been able to find any information that provides a solution to the problem.After checking the checkboxes in the form indicating which people are going to be sitting at this table and submitting the form and returning to the form I find that the checkboxes are no longer checked???

这是我的模型文件的内容

here are the contents of my model files

# 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

为简洁起见,这里是省略部分的表格.

Here is the form with parts omitted for brevity.

<%= 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 中实现的复选框列表.

And here is the checkbox list I have implemented within the 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

我如何以这种形式对复选框进行持续检查?

How do I get the checkboxes to be persistently checked in this form?

推荐答案

您可以使用 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 %>

它还将保留数据.

这篇关于Rails中的嵌套复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆