我安装了Devise身份验证没有问题。现在我尝试使用Omniauth-facebook添加一个用Facebook登录的选项。
我按照this guide中的说明操作,但我收到了有关遗失的错误" Passthru"访问网址localhost:3000/auth/facebook
时的文档。
这是我收到的第一个错误:
Unknown action
The action 'passthru' could not be found for RegistrationsController
我尝试了一个简单的修复,只需添加一个空的" passthru"行动到我的控制器:
def passthru
end
这解决了这个错误,但我得到了另一个错误:
Template is missing
Missing template registrations/passthru, devise/registrations/passthru, devise/passthru, application/passthru with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * "/home/user/project/app/views" * "/home/user/.rvm/gems/ruby-2.0.0-p648@railstutorial_rails_4_0/gems/devise-3.5.2/app/views"
我尝试创建一个" passthru.html.erb"在所述文件夹中,但该错误仍然存在。无论如何,我认为这些错误是更深层次问题的象征。
还有其他人遇到过这个问题吗?我能找到的只有this SO question,但没有一个答案是有帮助的。
的Gemfile
gem 'devise'
gem 'omniauth-facebook'
gem 'omniauth'
的routes.rb
devise_for:成员,控制者:{注册:'注册',omniauth_callbacks: 'registrations' }
member.rb
devise :database_authenticatable, :registerable,
:omniauthable, :omniauth_providers => [:facebook]
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |member|
member.email = auth.info.email
member.password = Devise.friendly_token[0,20]
member.title = auth.info.name
end
end
registrations_controller.rb
def facebook
@member = Member.from_omniauth(request.env["omniauth.auth"])
if @member.persisted?
sign_in_and_redirect @member, :event => :authentication
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_member_registration_url
end
end
def failure
redirect_to root_path
end
def passthru
end
初始化/ devise.rb
config.omniauth :facebook, "<app_id>", "<app_secret>"
答案 0 :(得分:2)
试试这个:
更新GemFile:
ul.insidenav li
转到rails_apps / yourapp / config / initializers / devise.rb
gem 'omniauth-facebook'
gem 'omniauth'
更新用户模型
Devise.setup do |config|
config.omniauth :facebook, "KEY", "SECRET"
end
转到:rails_apps / yourapp / config / routes.rb
class User < ActiveRecord::Base
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|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
end
end
end
在视图中修改
Rails.application.routes.draw do
devise_for :users
resources :users
end
答案 1 :(得分:1)
Passthru是来自omniauth的遗物更新你的设计omniauth的宝石等等。 有一个名为omiauth_callback的控制器,这是一个制造噪音的人; P。(可以帮助你追踪问题的来源)
如果你在控制器中创建一个方法,如下所示:
def passthru
end
您必须使用(甚至为空)或重定向创建视图:从ajax技术中获取灵感来绕过html渲染。
希望它能帮助你解决问题。
尝试这些路线: ``` user_omniauth_authorize /users/auth/:provider(.:format)sessions#passthru {:provider =&gt; / facebook | twitter | google /}
user_omniauth_callback /users/auth/:action/callback(.:format)sessions#(? - mix:facebook | twitter | google) ```
答案 2 :(得分:0)
您可以为write
Omniauth callback
您需要将路线重置为
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
@user = User.from_omniauth(request.env['omniauth.auth'])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def after_sign_in_path_for(resource)
super resource
end
end
我记得你不需要在devise_for :members, :controllers => {:registrations => "members/registrations", :omniauth_callbacks => 'omniauth_callbacks'}
:omniauth_providers => [:facebook]
现在,您可以在member.rb
中添加一个按钮,或者在sign_up page
中添加以下代码,因为它也会在您的sign_in表单中提供。
devise/shared/_links.html.erb
您还需要在初始化程序中配置设计
在<%- if devise_mapping.omniauthable? %>
<%- resource_class.omniauth_providers.each do |provider| %>
<%= link_to "Sign up with #{provider.to_s.titleize}", omniauth_authorize_path(resource_name, provider), class: "btn btn-default navbar-btn" %><br />
<% end -%>
<% end -%>
/config/initializers/devise.rb
请使用facebook Link
浏览Sing_up这个最简单的教程答案 3 :(得分:0)
试试这个.........
<强>配置/初始化/ devise.rb 强>
//this header is only required for ajax..
header("Access-Control-Allow-Origin: *");
require('../wp-load.php');
if(isset($_REQUEST['name'];))
{
echo wp_create_category($_REQUEST['name'],0);
}
else
{
echo false;
}
<强>配置/ routes.rb中强>
config.omniauth :facebook, ENV["FACEBOOK_KEY"], ENV["FACEBOOK_SECRET"], { :scope => 'email, offline_access'}
应用/模型/ member.rb 强>
devise_for :members, controllers: { registrations: 'registrations', omniauth_callbacks: "omniauth_callbacks" }
应用/控制器/ omniauth_callbacks_controller.rb 强>
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |member|
member.email = auth.info.email
member.password = Devise.friendly_token[0,20]
member.title = auth.info.name
end
end
希望这对你有用。