错误Ruby on Rails:Users :: OmniauthCallbacksController#facebook缺少此请求格式和变体的模板
问题描述:
我试图添加一个外部登录与Facebook,但每当我意识到主页正确指示我到Facebook页面,但后来我得到以下错误,并不明白它可能是什么。错误Ruby on Rails:Users :: OmniauthCallbacksController#facebook缺少此请求格式和变体的模板
“Users :: OmniauthCallbacksController#facebook缺少此请求格式和变体的模板request.formats:[”text/html“] request.variant:[]注意!对于XHR/Ajax或API请求,动作通常会以204无内容回应:一个空的白色屏幕由于您正在将其加载到网络浏览器中,因此我们认为您期望实际渲染模板而不是无效,因此我们显示的错误是额外的,如果你期待204没有内容,继续下去,这就是你从XHR或API请求中得到的结果,给它一个机会。“
"That's what you'll get from an XHR or API request. Give it a shot."
raise ActionController::UnknownFormat, message
else
logger.info "No template found for #{self.class.name}\##{action_name}, rendering head :no_content" if logger
super
这是我的控制器
class Users::OmniauthCallbacksController < ApplicationController
def facebook
@User = User.from_omniauth(request.env["omniauth.auth"])
if @User.persisted?
@User.remember_me = true
sign_in_and_redirect @User, event: :authentication
end
end
end
这是用户模式。
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,:omniauthable, :omniauth_providers => [:facebook]
def self.from_omniauth(auth)
where(provider: auth[:provider], uid: auth[:uid]).first_or_create do |user|
if auth[:info]
user.email = auth[:info][:email]
user.name = auth[:info][:name]
end
user.password = Devise.friendly_token[0,20]
end
end
has_many :articles , :dependent => :destroy
end
和我把配置/初始化/ divise.rb此行
config.omniauth :facebook, '504432376574467', 'b2bb80641fcc2ca4d28e48c5ce*******'
答
我的猜测是,User.from_omniauth
无法创建用户(可能是由于user.password
和user.password_confirmation
不匹配),这导致Users::OmniauthCallbacksController#facebook
到达方法的末尾,而不在if
子句内。
要检查,你可以例如添加一个else
子句到你的Facebook回调和raise
在那里的错误。