我正在使用omniauth-twitter gem进行身份验证。我的rails应用程序由Mongodb支持。
点击登录后,它会打开Twitter个人资料。我输入了凭据并被重定向回应用程序,并显示错误
unknown operator: $oid (2)
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end
helper_method :current_user
它指向@current_user
行的错误。但是当我检查数据库中通过执行User.last
创建的用户时,我可以看到一个用户对象。
User.rb 文件
class User
include Mongoid::Document
field :provider, type: String
field :uid, type: String
field :name, type: String
field :location, type: String
field :image_url, type: String
field :url, type: String
index({ provider: 1, uid: 1 }, { unique: true})
class << self
def from_omniauth(auth_hash)
user = find_or_create_by(uid: auth_hash['uid'], provider: auth_hash['provider'])
user.name = auth_hash['info']['name']
user.location = auth_hash['info']['location']
user.image_url = auth_hash['info']['image']
user.url = auth_hash['info']['urls']['Twitter']
user.save!
user
end
end
end
然后我创建了一个会话控制器,如下所示。
class SessionsController < ApplicationController
def create
begin
@user = User.from_omniauth(request.env['omniauth.auth'])
session[:user_id] = @user.id
flash[:success] = "Welcome, #{@user.name}!"
rescue
flash[:warning] = "There was an error while trying to authenticate you..."
end
redirect_to root_path
end
def destroy
if current_user
session.delete(:user_id)
flash[:success] = 'See you!'
end
redirect_to root_path
end
end
有人可以帮我解决这个问题吗?