grouped_collection_select返回错误

时间:2012-01-06 12:46:34

标签: ruby-on-rails ruby-on-rails-3

我有一个" grouped_collection_select"对于"分支"领域,与一个"行业"领域。 Sector has_many分支和分支belongs_to sector:

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

这有效,但是&#34 ;:分支&#34;显示所有分支,并且应该只显示current_user.company的分支,就像sectord一样。但是当我改变&#34 ;:分支&#34;进入&#34; current_user.company.branches.order(:branch)&#34;,我收到错误:

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

我在这里做错了什么?谢谢!

1 个答案:

答案 0 :(得分:0)

为了清晰(和语义),在控制器中设置该集合以在视图中使用:

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

...或者如果您想限制用户为每个扇区显示的分支,您可能希望根据用户的分支构建集合。一个例子:

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

然后假设您要设置branch_id并且扇区具有name属性并且该分支具有name属性,这应该有效:

 <%= 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.