我正在为twitter实现全功能,我遇到了一个错误"找不到'id'=true的用户",错误指向应用程序控制器current_user
方法。以下是我的current_user
方法:
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
当我试图登录错误弹出,我不能通过登录加载我的索引页。
我认为你的问题应该通过以下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
我终于解决了这个问题,问题是我已经删除了数据库中以前经过身份验证的twitter用户,我正试图在应用程序上再次使用相同的凭据进行身份验证。
所以我所做的是创建一个新的twitter应用程序,并使用不同的密钥验证到我的rails应用程序…希望这说明了一切,谢谢