我在/ auth / twitter上使用omniauth gem提供“使用twitter登录”链接。
当我在开发环境中运行项目时,一切似乎都很好。但是我在尝试登录生产时遇到错误。在我运行rails s -e production并点击“使用Twitter登录”按钮
之后在2014-08-25 09:42:15 +0200开始获取127.0.0.1的GET“/ auth / twitter” I,[2014-08-25T09:42:15.959278#5144] INFO - omniauth:(twitter)请求阶段已启动。
开始GET“/ auth / twitter / callback?oauth_token = BnjEPgOdMrOMMSE **************& oauth_verifier = mXD2WICHMs1k6z04UJ ***********”for 127.0。 0.1分,电话:2014-08-25 09:42:17 +0200 I,[2014-08-25T09:42:17.920926#5144] INFO - omniauth:(twitter)回调阶段已启动。
SessionsController处理#create as HTML 参数:{“oauth_token”=>“lUv4mg4F *************”,“oauth_verifier”=>“l0i8DN ********”,“提供者”=> “推”}
SQLite3 :: SQLException:没有这样的表:users:SELECT“users”。* FROM“users”WHERE“users”。“provider”='twitter'和“users”。“uid”= '27 *** **'ORDER BY'用户“。”id“ASC LIMIT 1
我尝试将用户表添加到db / migrate manualy并运行rake db:migrate,但这不起作用。
我的Gemfile文件:
source 'https://rubygems.org'
gem 'rails_12factor'
gem 'spork'
gem 'omniauth-twitter', '~> 1.0.1'
gem 'rb-readline'
gem 'rspec-rails'
gem 'rspec'
gem 'bootstrap-sass'
gem 'bootstrap'
gem 'twitter-bootstrap-rails'
gem 'twitter-text'
gem 'twitter', '~> 5.3.1'
gem 'sqlite3'
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'spring', group: :development
我的config / initializers / omniauth_twitter.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, Rails.application.config.twitter_key, Rails.application.config.twitter_secret
end
那是sessionsController #create
class SessionsController < ApplicationController
def create
begin
user = User.from_omniauth( env["omniauth.auth"] )
session[:user_id] = user.id
redirect_to root_path
rescue => ex
logger.error ex.message
end
end
...
end
和User.from_omniauth
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.oauth_token = auth.credentials.token
user.oauth_secret = auth.credentials.secret
user.save!
end
end
答案 0 :(得分:0)
您是否使用生产环境运行迁移?
rake db:migrate RAILS_ENV=production