我是 Rails 新手,我想允许用户使用 Devise 和 Omniauth 登录/注册我的网站。
所以我关注了 OmniAuth:概述(https://github.com/plataformatec/devise/wiki/OmniAuth%3a-Overview)。但我无法让它工作,当我尝试在 heroku 生产上测试它时,它显示如下所示的错误。
NoMethodError in Users::OmniauthCallbacksController#facebook
undefined method `find_for_facebook_oauth' for #<Class:0x007f9bc0a3aa30>
以下是我的代码,应该与 OmniAuth 相同:概述,任何帮助将不胜感激。
user.rb
class User < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
# 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, :address, :mobile, :provider, :uid
# attr_accessible :title, :body
has_many :pins, :dependent => :destroy
end
def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
user = User.where(: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
Devise.rb
# so you need to do it manually. For the users scope, it would be:
# config.omniauth_path_prefix = "/my_engine/users/auth"
config.secret_key = '75f6735f226e8ea0484f2abd55f78efee516306e8a0e69ac2cd68f50ce0f44078af9b3497bf49db3874a523c573b2e22eec01d508f703e32887de8c8f44740cb'
require "omniauth-facebook"
config.omniauth :facebook, "3223424", "52341341341134", :strategy_class => OmniAuth::Strategies::Facebook
{:scope => 'email, offline_access', :client_options => {:ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}}}
My Gemfiles
gem 'omniauth'
gem 'omniauth-facebook'
gem 'oauth2'
Routes.rb
Dine::Application.routes.draw do
get "home/index"
resources :pins
resources :pin
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
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'
get 'know' => 'pages#know'
get 'give' => 'pages#give'
get 'how' => 'pages#how'
match 'contact' => 'contact#new', :as => 'contact', :via => :get
match 'contact' => 'contact#create', :as => 'contact', :via => :post
match 'inform' => 'inform#new', :as => 'inform', :via => :get
match 'inform' => 'inform#create', :as => 'inform', :via => :post
as :user do
match '/user/confirmation' => 'confirmations#update', :via => :put, :as => :update_user_confirmation
end
devise_for :users, :controllers => { :confirmations => "confirmations" }
end
users/omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
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
end
再次感谢各位。