1

I want dropdown in User sign up form which will set the user role which belongs to a department. Here is are models

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

And in the registration form i am trying this:

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

This produces error undefined method `merge' for :title:Symbol

Department and Role both models contain "title"

I don't know where i am lacking

4

1 回答 1

1

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

或者

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

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

检查这个 SO 问题:collection_select 方法在 Rails 3.1.1 中给出错误

于 2014-03-17T11:26:03.307 回答