10

http://simple-form.plataformatec.com.br/#usage/collectionsに simple_form 2.0 の grouped_select 機能に関するドキュメントが少しあるようです。ドキュメントには次の行があります。

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

しかし、それはそれを機能させるのに十分なコンテキストを私に与えていないようです. これが私が持っているものです。

お問い合わせ、広告、挿入の 3 つのモデルがあります。

Ads has_many Insertions, and Insertions belongs_to Ads Inquiries belongs_to Insertion, and Insertions has_many Inquiries

このドロップダウンは照会ビュー用です。simple_form を使用する= f.input :insertion, :collection => @adsと、少なくともドロップダウンに広告タイトルのリストを出力できます。ad.title を optgroup として機能させたいと思います。次に、広告の挿入を選択可能なコンテンツとして提供したいと思います...次のようなものです:

<select>
  <optgroup label="Ad.Title">
    <option value="Ad.Insertion.id">Ad.Insertion.Title</option>
    <option value="Ad.Insertion.id">Ad.Insertion.Title</option>
    <option value="Ad.Insertion.id">Ad.Insertion.Title</option>
    <option value="Ad.Insertion.id">Ad.Insertion.Title</option>
  </optgroup>
  <optgroup label="Ad.Title">
    <option value="Ad.Insertion.id">Ad.Insertion.Title</option>
    <option value="Ad.Insertion.id">Ad.Insertion.Title</option>
    <option value="Ad.Insertion.id">Ad.Insertion.Title</option>
    <option value="Ad.Insertion.id">Ad.Insertion.Title</option>
  </optgroup>
</select>

この simple_form 機能を機能させるためのアドバイスはありますか? 本当に感謝します!

このプロパティを実行する方法について洞察があれば、アプリについて他に何か言えることがあれば教えてください。

前もって感謝します!

更新:次を使用して部分的に機能するものを取得できました:

= f.input(:insertion_id, :collection => Ad.order(:name), :as => :grouped_select, :group_method => :insertions)

これに関する問題は、私が知る限り、どの列を表示テキストとして使用するかを指定する方法がないことです。どんな意見でも歓迎します。

4

2 に答える 2

23

追加の調査の後、ドキュメントをよく見て、構文をいじって、探していたものを解決しました。

標準的な Railsgrouped_collection_selectは次のようになります。

= f.grouped_collection_select(:insertion_id, Ad.order(:name), 
                              :insertions, :name, :id, :title, 
                              include_blank: "Please Choose...")

これは、次の構文で simple_form 2.0 を使用してやり直すことができます。

= f.input(:insertion_id, :collection => Ad.order(:name),
                         :as => :grouped_select,
                         :group_method => :insertions,
                         :group_label_method => :name,
                         :label_method => :title,
                         :include_blank => "Please Choose...")

それが将来他の人々に役立つことを願っています。

于 2012-04-10T02:39:14.667 に答える
1

別の方法は次のとおりです。

<%= f.input :product_category do %>
    <%= f.select :product_category, grouped_options_for_select(Product.PRODUCT_CATEGORY), include_blank: true %>
  <% end %>

これは、アクティブなレコード モーダルを使用せずに optgroup 選択を形成しようとしている場合にうまく機能します。

参照: https://github.com/plataformatec/simple_form#wrapping-rails-form-helpers および http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-option_groups_from_collection_for_select

于 2015-02-08T11:57:56.627 に答える