AuthenticationsController中的NoMethodError #create

时间:2013-02-27 05:06:57

标签: ruby-on-rails-3 rubygems omniauth

似乎无法找到答案,我已经检查了所有堆栈溢出,这似乎是一个未解之谜。谁能解决这个问题就太棒了!

不确定要包含哪些控制器或内容,所以一旦你问,我会用它们更新OP。

希望我们能解决这个问题!

身份验证控制器

class AuthenticationsController < ApplicationController
  def create
    omniauth = request.env["omniauth.auth"]
    authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
    if authentication
      flash[:notice] = "Signed in successfully."
      sign_in_and_redirect(:user, authentication.user)
    elsif current_user
      current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'])
      flash[:notice] = "Authentication successful."
      redirect_to authentications_url
    else
      user = User.new
      user.apply_omniauth(omniauth)
      if user.save
        flash[:notice] = "Signed in successfully."
        sign_in_and_redirect(:user, user)
      else
        session[:omniauth] = omniauth.except('extra')
        redirect_to new_user_registration_url
      end
    end
  end
end

User.rb(用户模型)

class User < ActiveRecord::Base
  has_many :authentication

  # Include default devise modules. Others available are:
  # :token_authenticatable, :lockable, :timeoutable and :activatable
  devise :database_authenticatable, :registerable, 
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation
  # attr_accessible :title, :body

  def apply_omniauth(omniauth)
    self.email = omniauth['user_info']['email'] if email.blank?
    authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid'])
  end

  def password_required?
    (authentications.empty? || !password.blank?) && super
  end
end

AuthenticationsController中的NoMethodError #create

undefined method `[]' for nil:NilClass

app/models/user.rb:14:in `apply_omniauth'
app/controllers/authentications_controller.rb:14:in `create'

0 个答案:

没有答案