设计用户模型中未定义的方法

发布于 2024-10-12 22:55:03 字数 1158 浏览 5 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

弥枳 2024-10-19 22:55:03

正如 @njorden 所提到的,请务必执行以下操作:

  1. 迁移以将 department_id 添加到您的用户表
  2. attr_accessible :department_id 以允许通过编辑注册表单进行批量分配

As mentioned by @njorden, be sure to do the following:

  1. migrate to add department_id to your users table
  2. attr_accessible :department_id to allow mass assignment via edit registration form
活雷疯 2024-10-19 22:55:03

这确实应该有效,我的设备上有一个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?

桃酥萝莉 2024-10-19 22:55:03

这看起来倒退了。 collection_select 适用于关联的 has_many 端。请注意,这里的语义提示是,当一个对象属于另一个对象时,它是单数的。不涉及收集。您请求department并返回一个对象。从另一个方向来看,如果您的 Department 设置为 has_many :users,那么询问用户将为您提供一个用户列表。

请查看 select 的文档。他们给出了一个简单的例子,说明您试图对部门做什么(以选择博客文章作者的形式)。

That looks backwards. collection_select would be appropriate for the has_many side of the association. Note that the semantic hint here is that when an object belongs_to another object it's singular. There is no collection involved. You ask for department and get back a one object. From the other direction, if your Department was set to has_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).

甜心 2024-10-19 22:55:03

只是为了添加我的案例,我也遇到了类似的问题。最初每个用户都有一个带有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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文