Ruby on rails ActiveModel::rails 4中缺少AttributeError
这个问题中的代码在Rails 3中没有问题,但它在Rails 4中提出了一个ActiveModel::MissingAttributeError,它也使用Desive(用于Rails 4)。有三个相关模型,用户模型和其他两个模型之间存在多态关系 User.rbRuby on rails ActiveModel::rails 4中缺少AttributeError,ruby-on-rails,Ruby On Rails,这个问题中的代码在Rails 3中没有问题,但它在Rails 4中提出了一个ActiveModel::MissingAttributeError,它也使用Desive(用于Rails 4)。有三个相关模型,用户模型和其他两个模型之间存在多态关系 User.rb belongs_to :profile, polymorphic: true 律师简介 has_one :user, as: :profile, dependent: :destroy has_one :user, as: :p
belongs_to :profile, polymorphic: true
律师简介
has_one :user, as: :profile, dependent: :destroy
has_one :user, as: :profile, dependent: :destroy
LawStudentProfile.rb
has_one :user, as: :profile, dependent: :destroy
has_one :user, as: :profile, dependent: :destroy
用户填写相关注册表后,将在我在registrations controller中重写的Desive的After_sign_up_path_for(user)方法中为新用户创建一个新的律师或学生档案,如下所示
class RegistrationsController < Devise::RegistrationsController
def after_sign_up_path_for(user)
if user.lawyer === true
user.profile = LawyerProfile.create!
user.add_role :lawyer
user.save!
elsif user.student === true
user.profile = LawStudentProfile.create!
user.add_role :student
user.save!
else
...
显示此错误消息
ActiveModel::MissingAttributeError in RegistrationsController#create
can't write unknown attribute `profile_id'
由于我在Rails 3应用程序上工作,我猜这可能与强参数有关,但是,我从来没有在Rails 3应用程序的attr\u accessible
列表中添加profile\u id,所以我不能完全确定这是一个强参数问题。事实上,用户和律师profile模型都没有属性(我创建的或以其他方式可见的)称为profile_id
无论如何,在应用程序控制器中,根据设计说明,我添加了以下内容,为注册时的用户创建了可接受参数的白名单。我将:profile_id添加到此列表中,但我仍然得到了相同的错误
before_filter :configure_permitted_parameters, if: :devise_controller?
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :email, :lawyer, :student, :password) }
end
由于不完全清楚多态关联,我想可能是:profile_id是LawyerProfile的一个属性(即使我没有在Rails 3应用程序中将它添加到LawyerProfile.rbattr_accessible
),我的lawyerprofiles控制器中没有创建操作
问题
1) 如果错误消息的原因是我没有为LawyerProfile.rb模型创建属性的白名单(即:profile_id),那么如果我没有创建操作,如何在lawyerprofiles_控制器中执行该操作?我只使用LawyerProfile.create!那一次
2) 如果不是第一个问题中提到的原因,那么这个错误的另一个原因可能是什么
更新,
其中一个奇怪的方面是,用户模型和律师档案模型都没有
profile\u id
列。它们都只有id列,就像Rails 3版本一样。回答了我自己的问题。尽管Rails 3和Rails 4应用程序之间的代码完全相同,但Rails 4应用程序中出现了一个错误,因为我在用户表中没有profile_id(或profile_type)列。因为我没有在Rails 3应用程序中将它保存到db中,所以它没有引发错误。我也没有在Rails 4应用程序中将其保存到db中,但是Rails现在在这种情况下会引发一个错误