简体   繁体   English

使用omniauth的Facebook登录无法正常工作

[英]Facebook login with omniauth not working

I am newbie trying to add facebook login to my rails app using omniauth & 2 and omniauth-facebook. 我是新手,尝试使用omniauth&2和omniauth-facebook将facebook登录名添加到我的Rails应用程序中。 I followed the instructions on https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview . 我按照https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview上的说明进行操作。

But it is showing me the below error now, I have searched around the net and tried many different ways without much success, I have no idea what I am doing wrong, so can I someone help me with this? 但这现在向我显示了以下错误,我已经在网上搜索并尝试了许多不同的方法而没有取得太大的成功,我不知道自己在做错什么,因此有人可以帮助我吗? much appreciated. 非常感激。

Running on 继续

rails (3.2.12) Using oauth2 (0.8.1) Using omniauth (1.1.4) Using omniauth-oauth2 (1.1.1) Using omniauth-facebook (1.4.1) rails(3.2.12)使用oauth2(0.8.1)使用omniauth(1.1.4)使用omniauth-oauth2(1.1.1)使用omniauth-facebook(1.4.1)

Error: ActiveRecord::RecordNotFound in UsersController#show Couldn't find User with id=sign_up 错误:UsersController#show中的ActiveRecord :: RecordNotFound找不到ID = sign_up的用户

My code 我的密码

 class User < ActiveRecord::Base
 # Include default devise modules. Others available are:
 # :token_authenticatable, :confirmable,
 # :lockable, :timeoutable and :omniauthable
   devise :database_authenticatable, :registerable, :recoverable, :rememberable,                  :trackable, :validatable, :omniauthable, :omniauth_providers => [:facebook]

 # Setup accessible (or protected) attributes for your model
 attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :provider, :uid, :name
 attr_accessible :title, :date_of_birth, :firstName, :lastName, :mailId, :phone, :provider, :uid

protected

 def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
user = User.find(:provider => auth.provider, :uid => auth.uid).first
unless user
  user = User.create(name:auth.extra.raw_info.name,
                       provider:auth.provider,
                       uid:auth.uid,
                       email:auth.info.email,
                       password:Devise.friendly_token[0,20]
                       )
 end
 user
end  

def self.new_with_session(params, session)
super.tap do |user|
  if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
    user.email = data["email"] if user.email.blank?
  end
 end
end

 has_many :pins, :dependent => :destroy

end 结束

devise.rb devise.rb

 require 'devise/orm/active_record'
 require 'omniauth-facebook'

 config.omniauth_path_prefix = "/users/auth"
 config.omniauth :facebook,  "XXXX", "XXXX", {:client_options => { :ssl => { :verify => false } }}
 end

routes.rb routes.rb

Dine::Application.routes.draw do
 get "home/index"


 resources :pins
 resources :pin 
 resources :users

match '/auth/facebook' => 'omniauth#passthru'

devise_scope :user do
get '/users/auth/:provider' => 'users/omniauth_callbacks#passthru'

devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } , :strategy_class => OmniAuth::Strategies::Facebook
match 'users/:id' => 'users#show', as: :user

root :to => 'pins#index'
get 'about' => 'pages#about'
get 'weekly' => 'pages#weekly'
get 'shop' => 'pages#shop'
get 'service' => 'pages#service'
get 'privacy' => 'pages#privacy'
get 'test' => 'pages#test'
get 'recipies' => 'pages@recipies'  

match 'contact' => 'contact#new', :as => 'contact', :via => :get
match 'contact' => 'contact#create', :as => 'contact', :via => :post
end
end

OmniauthCallbacksController OmniauthCallbacksController

 class OmniauthCallbacksController < Devise::OmniauthCallbacksController

def passthru
send(params[:provider]) if providers.include?(params[:provider])
end

protected

def facebook
raise "Implement me for facebook"
end

def twitter
raise "Implement me for twitter"
end

private

def providers
["facebook", "twitter"]
end
end

UsersController UsersController

class UsersController < ApplicationController
 def show
 @user = User.find(params[:id])
 @pins = @user.pins.page(params[:page]).per_page(20)
 end
end

new.html.erb new.html.erb

<h2>登入</h2>

<%= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div class="form-inputs">
<%= f.input :email, :required => false, :autofocus => true %>
<%= f.input :password, :required => false %>
<%= f.input :remember_me, :as => :boolean if devise_mapping.rememberable? %>
</div>

 <%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>

<div class="form-actions">
<%= f.button :submit, "Sign in" %>
 </div>
<% end %>

<%= render "devise/shared/links" %>
  1. from a terminal: 从终端:

    bundle exec rake routes >> r.txt 捆绑程式执行耙路径>> r.txt

  2. open r.txt and look for the line containing users#show , note down the first term of that line, which should be something like user_show (let's call it the_route ). 打开r.txt并查找包含users#show的行,记下该行的第一项,该行应类似于user_show(我们将其the_route )。

  3. add _path to your route (eg. the_route_path ) and search your whole project for that string. 在路径中添加_path (例如the_route_path ),然后在整个项目中搜索该字符串。

  4. somewhere in your project (most likely in a view) there will be a link_to or redirect_to the_route_path and I guess somewhere you will be passing 'sign_up' as a :id param. 在项目的某个位置(最有可能在视图中),将存在link_toredirect_to the_route_path ,我想您将在某个位置将'sign_up'作为:id参数传递。

  5. that's where the mistake is. 那就是错误所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM