Checkbox 如何基于数组对象标记simple_form复选框?
Rails视图有两个参数Checkbox 如何基于数组对象标记simple_form复选框?,checkbox,simple-form,ruby-on-rails-5,Checkbox,Simple Form,Ruby On Rails 5,Rails视图有两个参数 list_of_attributes = ["a1", "a2", "a3", "a4"] user_selections = ["a2", "a4"] 我能够使用以下简单表单定义显示相应的复选框和任何相关的用户选择 <% list_of_attributes.each do |item| %> <label> <%= check_box_tag "user_selections[]", item, user_select
list_of_attributes = ["a1", "a2", "a3", "a4"]
user_selections = ["a2", "a4"]
我能够使用以下简单表单定义显示相应的复选框和任何相关的用户选择
<% list_of_attributes.each do |item| %>
<label>
<%= check_box_tag "user_selections[]", item, user_selections.include?(item) %>
<%= item %>
</label>
<% end %>
如何使用simple_form f.input语法定义上述行为?使用以下定义,我可以显示相应的复选框,但任何用户选择都不会被“选中”
<%= f.input key, as: :check_boxes, collection: list_of_attributes,
:label => key, class: "form-control" %>
键,类:“表单控件”%>
谢谢您的帮助。添加checked属性使f.input定义功能正常
<%= f.input key, as: :check_boxes,
collection: list_of_attributes,
:label => key,
checked: user_selections,
class: "form-control" %>
<% end %>
键,
选中:用户选择,
类:“窗体控件”%>
添加checked属性使f.input定义功能正常
<%= f.input key, as: :check_boxes,
collection: list_of_attributes,
:label => key,
checked: user_selections,
class: "form-control" %>
<% end %>
键,
选中:用户选择,
类:“窗体控件”%>
试试这个。这对我使用引导CSS很有效。检查my github repo中的第25行
试试这个。这对我使用引导CSS很有效。检查my github repo中的第25行
.input_checkbox input {
width: auto !important;
}