我有一个用户类
class User
include Mongoid::Document
include Mongoid::Timestamps
embeds_one :profile
和班级资料
class Profile
include Mongoid::Document
include Mongoid::Timestamps
embedded_in :user
我使用devise和mongoid来验证用户以及以下方法来存储用户信息:
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
# Store information in user's profile
user.profile.first_name = auth['info']['first-name']
user.profile.last_name = auth['info']['last-name']
user.profile.profile_picture = auth['info']['picture-url']
user.profile.linkedin = auth['info']['public-profile-url']
end
end
但我总是面临以下错误
未定义的方法`first_name ='为零:NilClass
答案 0 :(得分:1)
创建一个新的Profile对象,用户作为属性
profile = Profile.create({
:user => user,
:first_name => auth['info']['first-name'],
:last_name => auth['info']['last-name'], ....})