我正在使用Smart listing gem 用于实时过滤。 以下是一个没有提交和 url 的简单表单。
<%= smart_listing_controls_for(:search) do %>
<%= simple_form_for :search do |f| %>
<%= f.input :certificates, collection: Certificate.all, :as => :check_boxes, include_hidden: false, :input_html => {:multiple => true} %>
<% end>
<% end>
上面的代码生成多个以“证书 ID”作为值的复选框。 一旦选中其中一个复选框,智能列表就会向 Controller 发送带有参数的请求。
Parameters: {"utf8"=>"✓", "search_smart_listing"=>{"_"=>"1", "page"=>"", "per_page"=>"10"}, "authenticity_token"=>"z25rULU5JeeWcEZdpsy0+bz7OJFDWPmXrVGnzPvdG0cjM0ufpc3ydB9+5GywDQkUmcm6RGJnF0C4Yrd0sWpJ6g==", "search"=>{ "certificates"=>["6"]}}
问题是当我选择多个复选框时,证书数组仅具有最新值,而不是所有选定的复选框值。
此外,当选中和取消选中复选框时,参数中的证书数组值保持不变。如果取消选中该复选框,并且只希望证书数组仅包含所有选定的复选框值,我希望从参数中的证书数组中删除该值。
以下是为多个复选框之一生成的 html 代码。
<span class="checkbox">
<label for="search_certificates_5">
<input class="check_boxes required" type="checkbox" value="5" name="search[certificates][]" id="search_certificates_5">
Certificate 1
</label>
</span>
提前致谢:)
最佳答案
由于 smart_listing_controls_for
和 simple_form_for
都创建了一个表单,因此您可能遇到的一个问题是在表单内创建表单,并且这既不是推荐的也不是标准的。这可能会导致意想不到的结果。
也许尝试在没有 simple_form 帮助器的情况下执行此操作,如下所示(假设 Certificate
具有描述属性):
<%= smart_listing_controls_for(:search) do %>
<%= Certificate.all.each do |certificate| %>
<div class="checkbox">
<label class= "checkbox inline">
<%= check_box_tag 'search[certificates][]', certificate.id %>
<%= certificate.description %>
</label>
</div>
<% end %>
<% end %>
更新
此外,智能列表 gem 的当前版本 (v1.1.2) 存在一个问题,不允许使用数组输入。问题出在javascript code的这一部分。这是在当前主分支上修复的,并且最近在最新提交上进行了更新,如您所见 here .
要解决此问题,请使用 Gemfile 中的最新提交,如下所示:
gem 'smart_listing', :git => 'https://github.com/Sology/smart_listing.git', :ref => '79adeba'
更新Gemfile后捆绑安装
并再次尝试上述代码。
关于ruby-on-rails - Rails - 通过参数传递多个复选框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36729489/