2015-09-26 30 views
0

我正在为twitter推行omniauth,并且遇到错误“找不到用户有'id'= true”错误指向应用程序控制器current_user方法。 Heere是我current_user方法:找不到'id'= true的用户Omniauth - rails 4

class ApplicationController < ActionController::Base 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception 
    helper_method :current_user 

    private 
    def current_user 
    @current_user ||= User.find(session[:user_id]) if session[:user_id] 
    end 

end 

,这里是我的User型号:

class User < ActiveRecord::Base 
    def self.find_or_create_by_auth(auth_data) 
    user = where(provider: auth_data[:provider], uid: auth_data[:uid]).first_or_create 
    user.update(name: auth_data[:info][:name]) 
    end 
end 

终于SessionController低于:

class SessionsController < ApplicationController 
    def create 
    @user = User.find_or_create_by_auth(request.env["omniauth.auth"]) 
    session[:user_id] = @user 
    redirect_to products_path, notice: "logged in as " 
    end 

    def destroy 
    session[:user_id] = nil 
    redirect_to root_path, notice: "Goodbye!!" 
    end 
end 

当我试图登录错误持久性有机污染物我无法通过登录来加载我的索引页面。

回答

0

我觉得你的问题应该与以下修改中User.find_or_create_by_auth方法来解决:

def self.find_or_create_by_auth(auth_data) 
    # all previous code 

    # you should return user from here 
    # your current code returned true of false 
    user 
end 

你也应该保存@user.id在会议上,未满@user对象:

session[:user_id] = @user.id 
0

我终于解决了这个,问题是我已经删除了在数据库中的先前经过身份验证的twitter用户,并且我正试图在应用上再次使用相同的凭据进行身份验证。

所以我做了什么是创建一个新的Twitter应用程序,并使用不同的密钥进行身份验证到我的导轨应用程序......这个解释它谢谢