Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 表格中的复选框用于表单_Ruby On Rails_Ruby_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 表格中的复选框用于表单

Ruby on rails 表格中的复选框用于表单,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,RubyonRails4 我想用一排问题填满一张表。然后有一个复选框作为一列来选择要在我的表单中提交的问题。我有一个集合复选框,不确定如何将它们放在表格中,在表格中可以选择:问题ID 表格: <%= form_for(@test) do |f| %> <div class="field"> <%= f.label :name %><br> <%= f.text_field :name %> </div>

RubyonRails4

我想用一排问题填满一张表。然后有一个复选框作为一列来选择要在我的表单中提交的问题。我有一个集合复选框,不确定如何将它们放在表格中,在表格中可以选择:问题ID

表格:

<%= 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 %>




重置搜索 复选框标题 内容 类型 类别 产品 使用者 活跃的

其他字段助手将生成名称类似于
name=“foo[name]”
name=“foo[type]”
等的输入。您需要通过一个问题ID数组作为
的值“foo[question\u id]”
。我在这里使用了
foo
,因为我不知道@test对象的类是什么

我将删除您已有的
collection\u复选框(即删除以下内容):

由于输入的name属性末尾有“[]”,所以选中复选框的所有值都将组合到params[:foo][:question_id]中的单个数组中,从而在控制器调用时设置问题关联

@foo.update_attributes(params[:foo])

什么类型的对象是@test?@test=test.new(test_参数)lols。好的,那么在我的回答中用'test'代替'foo'。好的,我会尝试一下。我会把什么放在与f的形式_相关的地方?这不是一个很好的英语句子,所以我不确定你的意思,但你不需要在整个形式中都使用
f
。你可以随时制作自己的标签助手
f
只是一个快捷方式,但有时自己用一个_标记就更简单了。我得到了一个错误:未定义的局部变量或#的方法'question'。对不起,我刚刚注意到您使用的是
q
作为循环变量,而不是
question
。我将更新我的答案。我还更新了答案以检查该问题是否已被选中(第三个参数的计算结果应为true或false,以确定复选框是否已被选中)
<td><%= check_box_tag "foo[question_ids][]", q.id, @test.question_ids.include?(q.id) %></td>
@foo.update_attributes(params[:foo])