【问题标题】: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>

问题是我的收藏显示在这样的数组中:

如何让这组复选框改为下拉格式?

【问题讨论】:

  • 从文档中,您可以将一个块传递给f.association (github.com/plataformatec/simple_form/blob/master/lib/…),这样您就可以传递一个带有复选框、标签和&lt;br&gt; 的块。如果你想要更复杂的东西(比如 3 列均匀分布的复选框+标签),你需要手动遍历那些可能的作者。

标签: 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) %>
    

    希望对你有帮助, 干杯

    【讨论】:

      猜你喜欢
      • 2014-06-16
      • 1970-01-01
      • 2011-09-03
      • 2014-04-07
      • 1970-01-01
      • 2015-06-23
      • 2014-02-23
      • 2016-02-20
      • 2014-04-08
      相关资源
      最近更新 更多