【问题标题】:Rails - Make association check boxes appear in drop down list instead of arrayRails - 使关联复选框出现在下拉列表中而不是数组中
【发布时间】:2017-08-07 15:20:11
【问题描述】:
我正在使用 simple_form gem 来呈现复选框。这是我的代码:
<tr>
<th>Authors</th>
<td><%= f.association :authors, as: :check_boxes, label: false %></td>
</tr>
问题是我的收藏显示在这样的数组中:
如何让这组复选框改为下拉格式?
【问题讨论】:
标签:
ruby-on-rails
arrays
associations
simple-form
dropdown
【解决方案1】:
找到了解决办法!我发现了this blog post 并使用了item_wrapper_tag: 助手,我在f.association 代码中使用了它。这让我可以在一系列复选框周围放置一个 div。然后我给它一个类,这样我就可以使用 CSS 来设置这个 div 的样式了,inline-block。
<tr>
<th>Authors</th>
<td><%= f.association :authors, as: :check_boxes, item_wrapper_tag: :div, item_wrapper_class: "inline_block", label: false %></td>
</tr>
产生了这个结果。内容现在位于垂直列表而不是数组中。
我需要在 CSS 上多花点功夫才能将它包含在一个可滚动的框中,但总的来说,这个解决方案对我有用。
【解决方案2】:
你试过collection_select吗?
看起来像这样:
<%= collection_select(:author, :name, Author.all, :id, :name_with_initial, prompt: true) %>
希望对你有帮助,
干杯