【问题标题】:How to create a grouped select box using simple_form?如何使用 simple_form 创建分组选择框?
【发布时间】:2011-05-01 13:17:27
【问题描述】:

我正在使用 simple_form gem 来创建 Rails 表单。 http://github.com/plataformatec/simple_form

一切都很好,除了如何创建分组选择框?在文档中或通过 google-ing 找不到它。

【问题讨论】:

  • simple_form 2.0 中添加了分组选择框。

标签: ruby-on-rails forms select simple-form


【解决方案1】:

我发现创建分组选择框的唯一合理方法是使用传递 grouped_options_for_select 的选择助手,确实为选择参数采用 selected_key 参数(这样您就可以确保你的模型中设置的那个实际上被选中了)。您将在下面看到完整的通话。很抱歉,如果它令人困惑。

-# @answer is the model instance passed into simple_form_for/form_for
select(@answer.class.to_s.underscore, :question_id, option_groups_from_collection_for_select(@categories, 'questions.order(:display_order)', :name, :id, :question, @answer.question_id))

如果有更好的方法来选择正确的值,我也会全力以赴。

tl;dr: 没有看到任何使用 form_for 或 simple_form_for 创建分组选择的方法,以上内容至少应该有所帮助。

【讨论】:

    【解决方案2】:

    我也刚刚查看了测试。 如果您想将 不同的值 传递给选项标签,请使用以下命令将其传递给集合:

    Agent = Struct.new(:id, :name)
    agents = [["First", []], ["Second", [Agent.new(7, 'Bond'), Agent.new(47, 'Hitman')]]]
    

    https://github.com/plataformatec/simple_form/blob/master/test/inputs/grouped_collection_select_input_test.rb

    【讨论】:

      【解决方案3】:

      如果你有两个模型是类别,子类别如下:

      class Category < ActiveRecord::Base
          has_many :products
          has_many :subcategories
      end
      
      class Subcategory < ActiveRecord::Base
          belongs_to :category
          has_many :products
      end
      

      然后就可以使用了

      <%= simple_form_for [:admin, @yourmodel] do |f| %>
          <%= f.input :subcategory_id, collection: Category.all, as: :grouped_select, group_method: :subcategories, prompt: "Select One" %>
          <%= f.submit "Submit" %>
      <% end %>
      

      这样的结果:

      <div class="form-group grouped_select optional yourmodel_subcategory_id">
          <label class="grouped_select optional control-label" for="yourmodel_subcategory_id">Subcategory</label>
          <select class="grouped_select optional form-control" id="yourmodel_subcategory_id" name="yourmodel[subcategory_id]">
          <option value="">Select One</option>
          <optgroup label="Your 1st Category">
              <option value="This subcategory id">one subcategory belongs to Your 1st Category</option>
          </optgroup>
          <optgroup label="Your 2nd Category">
              <option value="This subcategory id">one subcategory belongs to Your 2nd Category</option>
          </optgroup>
          </select>
      </div>
      

      希望这会有所帮助。

      【讨论】:

      • 哦.. 天哪。先生,你真是个奇迹。我刚刚花了最后一个小时将一些东西放在一起以获得数组中的数组,数组中的数组..根据选定的答案。然后我看到了你的解释,我在 20 秒内重复了同样的事情。太感谢了。我完全倒退了。
      【解决方案4】:

      这个问题很老,但无论如何它是“simple_form grouped select”谷歌搜索的最高结果,所以我认为下一位读者可能会受益于一些创造性的方法来使用最新的 simple_form(取自测试,这始终是确实是最好的文档)

      <%= f.input :author,
       :as => :grouped_select,
       :collection => [['Authors', ['Jose', 'Carlos']], ['General', ['Bob', 'John']]],
       :group_method => :last %>
      
      <%= f.input :author,
       :as => :grouped_select,
       :collection => Proc.new { [['Authors', ['Jose', 'Carlos']], ['General', ['Bob', 'John']]] },
       :group_method => :last %>
      
      <%= f.input :author,
       :as => :grouped_select,
       :collection => { ['Jose', 'Carlos'] => 'Authors' },
       :group_method => :first,
       :group_label_method => :last %>
      
      <%= f.input :author,
       :as => :grouped_select,
       :collection => { 'Authors' => ['Jose', 'Carlos'] },
       :group_method => :last,
       :label_method => :upcase,
       :value_method => :downcase %>
      

      【讨论】:

      • 正确。感谢您提供几种不同的解决方案。我希望他能接受你的回答。
      • 我假设这是针对 simple_form 2.0+ 的?或者这是否可以像我在 2010 年尝试使用的那样与旧的 simple_form 一起使用。
      • 现金!第一个解决方案对我来说非常有效。非常感谢!
      • 您能解释一下为什么使用 :first 作为 group_method 吗?我正在努力解决它...
      • 这让我一时难过,所以这是我的看法。您可以将任何对象作为集合传入。哈希、数组、AR 对象等。它需要是可迭代的。其中的每个“事物”都成为一个组对象。在这个组对象上调用“组标签方法”,它返回的内容被用作组标签。在组对象上也调用了'group method',返回的是值对象。值对象需要是可迭代的。 Simple Form 循环遍历它,在每个元素上调用“标签方法”和“值方法”。这为我们提供了每个选择选项的标签和值。简单吧?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-15
      • 1970-01-01
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      相关资源
      最近更新 更多