表格中的复选框表格

时间:2014-05-19 16:07:35

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

Ruby on Rails 4

我想用一排问题来填充表格。然后有一个复选框作为列,以选择要在我的表单中提交的问题。我有一个collection_check_boxes,不知道如何将它们放在可以选择:question_ids的表中。

表格:

<%= form_for(@test) do |f| %>
  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :type %><br>
    <%= f.text_field :type %>
  </div>
  <div class="field">
    <%= f.label :question %><br>
    <%= f.collection_check_boxes :question_ids, Question.all.to_a.collect, :id, :content, {class: "form-control input-lg"} %>
  </div>

  <div id="container" style="width:1100px; margin-left: auto; margin-right: auto;">
  <button type="button" class="reset">Reset Search</button>
  <table width="100%" class="tablesorter">
  <thead>
  <tr>
    <th width="2%" class="filter-false">CHECKBOX Header</th>
    <th width="40%" data-placeholder="Search">Content</th>
    <th width="10%" data-placeholder="Search">Type</th>
    <th width="10%" data-placeholder="Search">Category</th>
    <th width="10%" data-placeholder="Search">Product</th>
    <th width="10%" data-placeholder="Search">User</th>
    <th width="8%" data-placeholder="Search">Active</th>
    </tr>
  </thead>

<tbody>
<%# f.Question.all. ? do |q| %>
<% @questions.each do |q| %>
  <tr>
    <td><% q.check_box_tag %></td>
    <td><%= q.content %></td>
    <td><%= q.question_type %></td>
    <td><%= q.category %></td>
    <td><%= q.Product.find(product_id).name %></td>
    <td><%= q.user_id %></td>
    <td><%= q.active %></td>
  </tr>
  <% end %>
</tbody>
</table>
<br>
</div>
<div class="actions">
  <%= f.submit "Create Test", id: "commit" %>
</div>
<% end %>

1 个答案:

答案 0 :(得分:2)

其他字段助手将生成名称为name="foo[name]"name="foo[type]"等的输入。您需要通过问号数组发送"foo[question_ids]"的值。我在这里使用了foo,因为我不知道你的@test对象的类是什么。

我会删除您已经拥有的collection_check_boxes(即删除以下内容):

<div class="field">
  <%= f.label :question %><br>
  <%= f.collection_check_boxes :question_ids, Question.all.to_a.collect, :id, :content, {class: "form-control input-lg"} %>
</div>

然后,在您重复问题的部分,每个问题一行,将check_box_tag更改为

<td><%= check_box_tag "foo[question_ids][]", q.id, @test.question_ids.include?(q.id) %></td>

因为&#34; []&#34;在输入的name属性的末尾,复选的复选框的所有值将组合成params [:foo] [:question_ids]中的单个数组,因此在控制器调用时将设置问题关联

@foo.update_attributes(params[:foo])