我在new.html.erb中使用simple_form的collection_check_boxes.默认情况下,当渲染html时,它会在末尾添加隐藏字段,因为在控制器端我得到的值数组,最后一个元素为"".任何人都可以帮助我如何防止隐藏字段在视图上呈现?
new.html.erb:
<%= f.collection_check_boxes :topic_id, Topic.all, :id, :name %> <%= f.button :submit %>
我使用Firebug来检查我的checkboxes元素,结果如下:
我想删除上面的隐藏字段.请帮忙.
对于那些谁不希望删除空值,使用include_hidden: false
PARAM:
<%= f.collection_check_boxes :topic_id, Topic.all, :id, :name, include_hidden: false %>
它目前没有在API中记录,但它在那里:https://github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/tags/collection_check_boxes.rb
# Append a hidden field to make sure something will be sent back to the # server if all check boxes are unchecked. if @options.fetch(:include_hidden, true) rendered_collection + hidden_field else rendered_collection end