simple_form如何在rails中传递参数?

时间:2018-05-02 17:47:13

标签: ruby-on-rails ruby simple-form

我想通过simple_form,

批量更新记录

enter image description here

代码如下:

= simple_form_for url: batch_update_admin_tuitions_path, method: 'put' do |f|
  table.table
    thead
      tr
        th.text-center
        th.text-center column1
        th.text-center column2
        th.text-center column3
    tbody.text-center
      - @tuitions.each do |tuition|
        tr
          td = f.input_field "user_ids[]", as: :boolean, value: tuition.user_id
          td = @users_hash[tuition.user_id].name
          td = tuition.num
          td = tuition.sum_amount
  = f.submit :Submit, class: 'btn btn-success'

但它不起作用, ```

参数:

{"utf8"=>"✓", "authenticity_token"=>"FDpxyq9LE8UjxBjIm4lstj29N86Xlja+czWZXDdMcnehgBzmh0zz7aE5ZKqGWCWtX/h+iRwnTE048IPPodG10w==", "/admin/tuitions/batch_update"=>{"user_ids"=>[]}, "commit"=>"Submit"}

我发现了一个问题:user_ids为空。如何将user_ids传递给params?谁有好主意?先谢谢!

1 个答案:

答案 0 :(得分:0)

从SimpleForm文档中,提供了一些帮助程序:

collection_check_boxes:

form_for @tuition do |f|
  f.collection_check_boxes :user_ids, User.all, :id, :name
end

if you have an association,您可以使用as: :check_boxes

<%= simple_form_for @tuition do |f| %>
  <%= f.association :users, as: :check_boxes %>
  <%= f.button :submit %>
<% end %>

更一般地说,我认为SimpleForm使用f.input而不是f.input_field,因此您的代码应为:

td = f.input "user_ids[]", as: :boolean, value: tuition.user_id