所以我已经安装了Omniauth gem,它现在是用Devise实现的。
设计是很好的运作,然而,获得Omniauth设置已经是一场战斗,这是我仍在努力工作。我正在使用Omniauth和Twitter身份验证。
问题:当我点击‘登录twitter’图标时,它会把我重新引导到twitter,然后我会被提示进入我的twitter credntials..all,直到它开始重定向(回调)。
当它试图重定向到我的应用程序时,我得到以下错误:
NoMethodError in AuthenticationsController#create
undefined method `[]' for nil:NilClass
app/models/user.rb:14:in `apply_omniauth'
app/controllers/authentications_controller.rb:14:in `create'注册控制器:
class RegistrationsController < Devise::RegistrationsController
def create
super
session[:omniauth] = nil unless @user.new_record?
end
private
def build_resource(*args)
super
if session[:omniauth]
@user.apply_omniauth(session[:omniauth])
@user.valid?
end
end
end身份验证控制器:
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
endUser.rb (用户控制器)
class User < ActiveRecord::Base
has_many :authentications
# 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请阅读最新情况:
我刚刚遇到了下面的RailsCast。 本教程说要运行: rails g nifty:脚手架身份验证user_id:integer \ provider:string :string创建破坏 但是我的机器上没有漂亮的脚手架,我只是跑了 rails g脚手架身份验证user_id:integer \ provider:string :string创建破坏 不同的行为方式。它没有创建存根“索引”、“创建”和“破坏”控制器方法,而是在数据库中创建字段。 我怎样才能移除这些字段?
发布于 2013-02-28 06:59:10
在您的User.rb行#14中
而不是omniauth['user_info']['email'],它应该是omniauth['info']['email']
对于漂亮发电机,你应该有
gem "nifty-generators", group: :development
在你的Gemfile
尝试rails g scaffold --help查看脚手架生成器的可用选项
您也可以看看这个令人惊叹的rails-cast也是发电机-钢轨-3。
发布于 2013-02-28 06:53:01
试着替换
omniauth['user_info']['email']通过
omniauth['email']您可以使用这样的包装方法
更新:全方位包装器
def omni_conversion(omniauth)
{
# Required For Social Network Creation
access_token: omniauth.credentials.token,
link: omniauth.extra.raw_info.link,
provider: omniauth.provider,
providerid: omniauth.uid,
# Required For User Creation
birthday: omniauth.extra.raw_info.birthday,
email: omniauth.info.email,
first_name: omniauth.info.first_name,
gender: omniauth.extra.raw_info.gender,
last_name: omniauth.info.last_name,
middle_name: omniauth.info.middle_name,
picture: omniauth.info.image,
timezone: omniauth.info.timezone,
username: omniauth.extra.raw_info.username
}
end
omniauth = omni_conversion(omniauth)优点是你可以直接使用符号。你可以直接把它们传递给模特。
https://stackoverflow.com/questions/15129147
复制相似问题