设计用户模型中未定义的方法
我对 Rails 还很陌生,并尝试向我的 Devise User 模型添加 own_to 关联。 尝试渲染视图时出现的错误:
NoMethodError in Devise/registrations#edit undefined method `department_id' for #
在我看来,这个错误发生在 collection_select 上。这个方法不是belongs_to协会提供的吗?
用户模型
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable and :timeoutable
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
belongs_to :department
end
编辑视图
%h2
Edit #{resource_name.to_s.humanize}
= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f|
= devise_error_messages!
%p
= f.label :email
%br/
= f.text_field :email
%p
= f.label :department
%br/
= collection_select(resource_name, :department_id, Department.all, :id, :name)
%p
...
I'm fairly new to rails and trying to add a belongs_to association to my Devise User model.
The error I get when trying to render the view:
NoMethodError in Devise/registrations#edit
undefined method `department_id' for #
This error is occurring on the collection_select in my view. Isn't this method provided by the belongs_to association?
User model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable and :timeoutable
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
belongs_to :department
end
Edit view
%h2
Edit #{resource_name.to_s.humanize}
= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f|
= devise_error_messages!
%p
= f.label :email
%br/
= f.text_field :email
%p
= f.label :department
%br/
= collection_select(resource_name, :department_id, Department.all, :id, :name)
%p
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如 @njorden 所提到的,请务必执行以下操作:
department_id
添加到您的用户表attr_accessible :department_id
以允许通过编辑注册表单进行批量分配As mentioned by @njorden, be sure to do the following:
department_id
to your users tableattr_accessible :department_id
to allow mass assignment via edit registration form这确实应该有效,我的设备上有一个belongs_to:business用户模型,并且类似的collection_for_select效果很好。我唯一能想到的是,也许您的迁移尚未运行,并且您还没有 user.department_id 列?
This really should work, I have a belongs_to :business on my devise User model and an analogous collection_for_select works great. The only thing I can think of is that maybe your migrations haven't run and you don't have the user.department_id column yet?
这看起来倒退了。
collection_select
适用于关联的has_many
端。请注意,这里的语义提示是,当一个对象属于
另一个对象时,它是单数的。不涉及收集。您请求department
并返回一个对象。从另一个方向来看,如果您的Department
设置为has_many :users
,那么询问用户将为您提供一个用户列表。请查看
select
的文档。他们给出了一个简单的例子,说明您试图对部门做什么(以选择博客文章作者的形式)。That looks backwards.
collection_select
would be appropriate for thehas_many
side of the association. Note that the semantic hint here is that when an objectbelongs_to
another object it's singular. There is no collection involved. You ask fordepartment
and get back a one object. From the other direction, if yourDepartment
was set tohas_many :users
then asking for users would give you a list of users.Take a look at the docs for
select
. They give a simple example of what you're trying to do with departments (in the form of choosing an author for a blog post).只是为了添加我的案例,我也遇到了类似的问题。最初每个用户都有一个带有department_id的部门,然后我转移到has_many,并忘记从用户中删除department_id。解决方案是删除用户的department_id。
Just to add my case, I had a similar issue. Initially each user had a department with a department_id, then I moved to a has_many, and forgot to remove the department_id from user. Solution was to delete department_id off of user.