【问题标题】:Create embedded document on object creation in Mongoid during authentication with Devise an Mongoid在使用 Devise an Mongoid 进行身份验证期间在 Mongoid 中创建对象创建嵌入式文档
【发布时间】:2015-01-22 06:17:41
【问题描述】:

我有一个类用户

  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

但我总是面临以下错误

nil:NilClass 的未定义方法 `first_name='

【问题讨论】:

    标签: ruby-on-rails devise mongoid omniauth


    【解决方案1】:

    创建一个新的 Profile 对象,以用户为属性

    profile = Profile.create({
      :user => user,
      :first_name => auth['info']['first-name'],
      :last_name => auth['info']['last-name'], ....})
    

    【讨论】:

    • profile = Profile.new(:email => auth.info.email, :first_name => auth.info.first_name, :last_name => auth.info.last_name,...) user.profile = profile 也在工作,对我来说似乎更自然
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多