0
我试图找出如何创建刚刚创建的用户一个新的配置文件后,的Rails试图创建一个配置文件的用户已经创建
我使用设计的用户模型,并且用户模型与UserProfile模型具有一对一的关系。
这里是我的用户模型是什么样子:
class User < ActiveRecord::Base
has_and_belongs_to_many :roles
belongs_to :batch
has_one :user_profile
after_create :create_user_profile
def create_user_profile
self.user_profile.new(:name => 'username')
end
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
这产生了以下错误:
undefined method `new' for nil:NilClass
我试过User.find(1).user_profile在轨Ç,这工作,所以我很确定关系设置正确,
我可能是一个大胖胖的noob,并试图取自我不正确。
加你也可以告诉我如何访问模型中的参数......甚至可能吗?
感谢约翰...为我工作...你有任何想法如何,我可以使用PARAMS添加该名称而不是输入一个静态值?我想为用户的新表单添加一个用户名字段...可能吗? –
你可以放任何你想要的东西。我会更新答案 –
哦thankyou :)。 –