2016-07-14 15 views
12

Tôi đã theo dõi omniauth + devise integration guide for facebook và tôi vẫn gặp phải lỗi này khi tôi nhấp vào liên kết để đăng nhập bằng liên kết facebook.Devise + Omniauth: phương pháp chưa xác định `user_omniauth_authorize_path '

phương pháp xác định user_omniauth_authorize_path cho #<#<Class:0x0000000253a550>:0x000000035ea490>

Tôi đã hai lần kiểm tra mã trong hướng dẫn và tôi dường như không thể tìm ra những gì vấn đề là. gemfile tôi:

source 'https://rubygems.org' 

gem 'sendgrid' 
gem 'omniauth-facebook' 
gem 'gravatar' 
gem 'will_paginate' 
gem 'faker' 
gem 'devise' 
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 
gem 'rails', '4.2.6' 
# Use postgresql as the database for Active Record 
gem 'pg', '~> 0.15' 
# Use SCSS for stylesheets 
gem 'sass-rails', '~> 5.0' 
# Use Uglifier as compressor for JavaScript assets 
gem 'uglifier', '>= 1.3.0' 
# Use CoffeeScript for .coffee assets and views 
gem 'coffee-rails', '~> 4.1.0' 
# See https://github.com/rails/execjs#readme for more supported runtimes 
# gem 'therubyracer', platforms: :ruby 

# Use jquery as the JavaScript library 
gem 'jquery-rails' 
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks 
gem 'turbolinks' 
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 
gem 'jbuilder', '~> 2.0' 
# bundle exec rake doc:rails generates the API under doc/api. 
gem 'sdoc', '~> 0.4.0', group: :doc 

# Use ActiveModel has_secure_password 
# gem 'bcrypt', '~> 3.1.7' 

# Use Unicorn as the app server 
# gem 'unicorn' 

# Use Capistrano for deployment 
# gem 'capistrano-rails', group: :development 

group :development, :test do 
    # Call 'byebug' anywhere in the code to stop execution and get a debugger console 
    gem 'byebug' 
end 

group :development do 
    # Access an IRB console on exception pages or by using <%= console %> in views 
    gem 'web-console', '~> 2.0' 
    gem 'letter_opener' 
    # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 
    gem 'spring' 
end 

OmniauthCallbacksController:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController 



def facebook 

    @user = User.from_omniauth(request.env["omniauth"]) 

    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 failure 
    redirect_to root_path 
    end 

end 

tài mẫu:

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] 

    has_many :likings, dependent: :destroy 
    has_many :comments, dependent: :destroy 
    has_many :posts, dependent: :destroy 
    has_many :active_relationships, class_name: "Relationship", 
            foreign_key: "follower_id", 
            dependent: :destroy 
    has_many :passive_relationships, class_name: "Relationship", 
            foreign_key: "followed_id", 
            dependent: :destroy 

    has_many :following, through: :active_relationships, source: :followed 
    has_many :followers, through: :passive_relationships, source: :follower 


    def feed 
    Post.where("user_id IN (?) OR user_id = ?", following_ids, id) 
    end 

    # follows user 
    def follow(other_user) 
    active_relationships.create(followed_id: other_user.id) 
    end 

    # unfollows user 
    def unfollow(other_user) 
    active_relationships.find_by(followed_id: other_user.id).destroy 
    end 

    def following?(other_user) 
    following.include?(other_user) 
    end 

    def self.from_omniauth(auth) 
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
     user.email = auth.info.email 
     user.password = Devise.friendly_token[0,20] 
    end 
    end 
end 
+0

Bạn có thể đăng Gemfile của mình không? Hướng dẫn sử dụng Devise để xác thực, nhưng bạn có thể chưa thêm nó? – oreoluwa

+0

đã thêm gemfile. Tôi tin Devise đang làm việc vì tôi có thể đăng ký và đăng nhập bằng ứng dụng của mình. –

Trả lời

18

Thử chạy tuyến cào và xem những gì các đường dẫn cho omniauth đang có. Tôi tin rằng họ đã thay đổi với phiên bản phát hành gần đây nhất. Hãy thử thay đổi

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

để

<%= link_to "Sign in with Facebook", user_facebook_omniauth_authorize_path %> 

và xem nếu mà làm việc cho bạn.

Rõ ràng những người giúp việc cho các tuyến đường omniauth đã thay đổi kể từ khi tuyến đường cào lệnh cho tôi trở

user_facebook_omniauth_authorize GET|POST /users/auth/facebook(.:format)   omniauth_callbacks#passthru 

và không như nó đã được vài tháng trước, khi tôi bắt đầu dự án.

user_omniauth_authorize   GET|POST /users/auth/facebook(:provider)   omniauth_callbacks#passthru 

Bạn cũng nên thay đổi dòng

@user = User.from_omniauth(request.env["omniauth"]) 

để

@user = User.from_omniauth(request.env["omniauth.auth"]) 

Đây là những gì làm việc cho tôi, hy vọng điều này sẽ giúp.

+0

Điều đó đã hiệu quả! Cảm ơn bạn rất nhiều nhưng bây giờ tôi nhận được một lỗi khác. NoMethodError trong người dùng :: OmniauthCallbacksController # facebook phương thức undefined 'provider 'cho nil: NilClass. –

+0

Bạn có thể đăng phương thức facebook của OmniauthCallbacksController như thế nào không? Ngoài ra, hãy đăng mô hình Người dùng của bạn –

+0

Bài đăng đã chỉnh sửa để cung cấp bộ điều khiển và mô hình. –

2

Nó có thể không phải là câu trả lời mọi người đang tìm kiếm, nhưng bạn luôn có thể chỉ định một phiên bản cũ của đá quý trong đá quý của bạn.

gem 'devise', '~> 4.1.1'

Điều này khiến trang web sản xuất của tôi và tôi phải hành động nhanh chóng. Điều này làm việc.

Các vấn đề liên quan