2012-01-06 2 views
0

"ブランチ"フィールドの "grouped_collection_select"と "sector"フィールドの組み合わせがあります。部門belongs_toのセクターにhas_manyの枝と枝:grouped_collection_selectがエラーを返します

<%= f.grouped_collection_select(:branch, current_user.company.sectors.order(:sector), :branches, :sector, :id, :branch, include_blank: true) %> 

これは動作しますが、「:枝は、」すべての分岐を示してばかりsectordのように、current_user.companyの枝を表示する必要があります。 :「枝」を私が変更された場合でも、

(eval):1: syntax error, unexpected $end group.#<ActiveRecord 

は、私がここで間違って何をやっている:「current_user.company.branches.order(:ブランチ)」を、私はエラーを取得しますか?ありがとう!明瞭さ(と意味)については

答えて

0

あなたのビューで使用するために、コントローラにそのコレクションを設定します。

@sectors = current_user.company.sectors.order(:sector) 

...かは、ユーザがセクタごとに示している枝を制限したい場合は、おそらくユーザーのブランチに基づいてコレクションを構築したいと考えています。一つの例:それは大丈夫作品

<%= f.grouped_collection_select(:branch_id, @sectors, :branches, :name, :id, :name, :include_blank=>true) 

#:branch_id is the attribute you will be setting on the form_builder's object. 
#@sectors is the collection of objects for the groups. 
#:branches is the method that will be called on each sector object. 
#:name is the method that will be called on each sector object to label the group. 
#:id is the method that will be called on each branch to set the option's value. 
#:name is the method that will be called on each branch to set the option's label. 
+0

、それは示しています

@sectors = current_user.company.branches.collect{|b| b.sector}.uniq 

は、その後、あなたがBRANCH_IDを設定したいと仮定すると、セクタは、name属性を持っており、分岐はname属性があり、これは動作するはずですデータベース内のすべてのブランチセクタのように、current_user.companyに属するブランチのみを表示する必要があります。しかし、branchesの代わりに@branchesを使うとエラーが返されます。 – John

+0

ブランチを使用することは、次の式と同じです。 "current_user.company.sectors.order(:sector).first.branches"覚えておいてください:branchesは、各セクタで呼び出されるメソッドの名前です。そのため、それは文字列またはシンボルだけである可能性があります。問題のようなサウンドは、そのユーザーのアクセス可能なブランチに基づいて@sectorsコレクションを制限したいということです。たとえば、sectors = current_user.company.branches.collect {| branch | branch.sector} .uniq – miked

+0

私の結論は、grouped_collection_selectは私が使用できるものではないということです。代わりに、私はcollection_selectを使用し、セクターリストボックスでの選択に基づいてjQueryで値をフィルターに掛けます。しかし、あなたの説明に感謝します! – John