【问题标题】:Rails Simple-Form group_methodRails 简单形式 group_method
【发布时间】:2012-08-15 09:40:37
【问题描述】:

我在 simple_form github repo 上发现了以下内容:

f.input :country_id, :collection => @continents, :as => :grouped_select, :group_method =>   :countries

引起我注意的是:group_method,它在创建一个根据数据库中的内容提供选项的选择框时非常有用。我唯一无法解决的是:group_method 期望的输入类型,以及该方法的放置位置。

例如,我想为表格列:product_type 创建一个选择框。我想我会用我的简单形式写这样的东西:

= f.input :product_type_contains, :collection => @products, :as => :grouped_select, :group_method => :product_types

:product_type 将是被调用的方法。但是我不知道我应该写什么样的方法,simple_form期望什么样的结果,如果我应该把它放在Product类中,Product.rb。任何帮助将不胜感激!

【问题讨论】:

    标签: ruby-on-rails simple-form


    【解决方案1】:

    根据test suitesimple_form 似乎期望您将与 grouped_options_for_select 一起使用的数组或哈希类型:

    test 'grouped collection accepts group_label_method option' do
      with_input_for @user, :tag_ids, :grouped_select,
        :collection => { ['Jose', 'Carlos'] => 'Authors' },
      :group_method => :first,
      :group_label_method => :last
      [...]
    
    test 'grouped collection accepts label and value methods options' do
      with_input_for @user, :tag_ids, :grouped_select,
        :collection => { 'Authors' => ['Jose', 'Carlos'] },
        :group_method => :last,
        :label_method => :upcase,
        :value_method => :downcase
      [...]
    

    大概,您可以在 Product.rb 上编写一个创建类似结构的类方法,或者甚至尝试使用 grouped_options_for_select(@products)...

    希望这能让你走上正确的道路。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      • 2020-12-13
      • 1970-01-01
      • 2016-04-20
      • 2018-04-12
      • 1970-01-01
      相关资源
      最近更新 更多