Ruby on rails 在设备注册时创建空配置文件
我曾在rails应用程序中使用Desive进行身份验证,我希望用户能够注册,注册后能够编辑自己的个人资料。我已经跟随了堆栈上的其他答案,但是当我尝试使用默认的设计注册表单注册时,我得到了这个错误Ruby on rails 在设备注册时创建空配置文件,ruby-on-rails,ruby-on-rails-3,devise,Ruby On Rails,Ruby On Rails 3,Devise,我曾在rails应用程序中使用Desive进行身份验证,我希望用户能够注册,注册后能够编辑自己的个人资料。我已经跟随了堆栈上的其他答案,但是当我尝试使用默认的设计注册表单注册时,我得到了这个错误 NoMethodError in Devise::RegistrationsController#create undefined method `create' for nil:NilClass app/models/user.rb:17:in `create_profile' 我的User.r
NoMethodError in Devise::RegistrationsController#create
undefined method `create' for nil:NilClass
app/models/user.rb:17:in `create_profile'
我的User.rb如下所示
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :profile
attr_accessible :login, :username, :email, :password, :password_confirmation, :remember_me, :profile_attributes
attr_accessor :login
accepts_nested_attributes_for :profile
validates :username, :uniqueness => { :case_sensitive => false }, :presence => true
after_create :create_profile
def create_profile
self.profile.create
end
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions).where(["username = :value OR lower(email) = lower(:value)", { :value => login }]).first
else
where(conditions).first
end
end
end
您需要使用
self.create\u profile
而不是self.profile.create
for有一个关联
范例
An Account class declares has_one :beneficiary, which will add:
Account#beneficiary (similar to Beneficiary.where(account_id: id).first)
Account#beneficiary=(beneficiary) (similar to beneficiary.account_id = account.id; beneficiary.save)
Account#build_beneficiary (similar to Beneficiary.new("account_id" => id))
Account#create_beneficiary (similar to b = Beneficiary.new("account_id" => id); b.save; b)
Account#create_beneficiary! (similar to b = Beneficiary.new("account_id" => id); b.save!; b)
更多文档在Desive::RegistrationsController#create中引发另一个错误SystemStackError。堆栈级别太深。它没有给出错误可能来自哪个文件的指示。User
和Profile
model之间必须来回触发循环回调。在rails控制台中检查它我将如何检查它?明白了,我在user.rb中更改了create_profile方法的名称以添加_profile。最后一个方法类似于def add_profile self.create_profile end
def edit
@profile = current_user.profile
end
An Account class declares has_one :beneficiary, which will add:
Account#beneficiary (similar to Beneficiary.where(account_id: id).first)
Account#beneficiary=(beneficiary) (similar to beneficiary.account_id = account.id; beneficiary.save)
Account#build_beneficiary (similar to Beneficiary.new("account_id" => id))
Account#create_beneficiary (similar to b = Beneficiary.new("account_id" => id); b.save; b)
Account#create_beneficiary! (similar to b = Beneficiary.new("account_id" => id); b.save!; b)