3

simple_form github リポジトリで次のようなものを見つけました。

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

私の注意を引いた:group_methodのは、データベースの内容に基づいてオプションを提供する選択ボックスを作成するときに非常に役立つということでした。私がうまくいかない唯一のことは、 が:group_method期待する入力の種類と、メソッドをどこに置くかです。

たとえば、テーブル column の選択ボックスを作成したいとします:product_type。簡単な形式で次のようなものを書くと思います。

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

どこ:product_typeで呼び出されているメソッドになります。しかし、どのようなメソッドを記述すればよいか、simple_form が期待する結果はどのようなものか、Product クラスに配置する必要があるかどうかはわかりませんProduct.rb。どんな助けでも大歓迎です!

4

1 に答える 1

2

テスト スイートによると、simple_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) を使用してみてください...

これがあなたを正しい道に導くことを願っています。

于 2012-09-11T18:33:39.737 に答える