实现grouped_collection_select不起作用

时间:2014-03-17 10:47:19

标签: ruby-on-rails ruby-on-rails-4 haml grouped-collection-select

我想在用户注册表单中下拉,该表单将设置属于某个部门的用户角色。这是模型

class Department < ActiveRecord::Base
  has_many :roles

end

class Role < ActiveRecord::Base
  belongs_to :department
  has_many :users
end

class User < ActiveRecord::Base
  belongs_to :roles
end

在注册表格中,我正在尝试这个:

=form.grouped_collection_select(:user, :role_id, @departments, :roles, :title, :id,:title)

这会产生错误 未定义的方法`merge&#39; for:title:Symbol

部门和角色两个模型都包含&#34; title&#34;

我不知道我缺少的地方

1 个答案:

答案 0 :(得分:1)

如果您从form.grouped_collection_select中删除表单,您的代码应该可以正常工作

OR

如果您确实需要它并且表单已经引用了用户表单构建器,那么您可以跳过第一个参数:user传递给grouped_collection_from方法

使用任何表单构建器来启动辅助方法(select,collection,grouped_collection,...等)将自动将其object传递给帮助程序,并且帮助程序将期望在{{1}之后开始的参数参数。

检查此问题:collection_select method gives error in Rails 3.1.1