我对 Rails 还很陌生,并试图将 belongs_to 关联添加到我的 Devise User 模型中。尝试渲染视图时出现的错误:
设计/注册中的 NoMethodError #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
...