【问题标题】:Devise + Omniauth: undefined method `user_omniauth_authorize_path'设计 + Omniauth:未定义的方法 `user_omniauth_authorize_path'
【发布时间】:2016-07-14 16:44:29
【问题描述】:

我一直在关注omniauth + devise integration guide for facebook,当我单击链接以使用 facebook 链接登录时,我不断收到此错误消息。

未定义方法user_omniauth_authorize_path 用于#<#<Class:0x0000000253a550>:0x000000035ea490>

我已经仔细检查了指南中的代码,但似乎无法弄清楚问题所在。我的宝石文件:

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

用户模型:

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

【问题讨论】:

  • 你能发布你的 Gemfile 吗?教程使用Devise进行身份验证,你可能没有添加?
  • 添加了 gemfile。我相信 Devise 可以正常工作,因为我可以使用我的应用注册和登录。

标签: ruby-on-rails facebook devise


【解决方案1】:

尝试运行 rake 路由并查看omniauth 的路径是什么。我相信它们已经随着最新版本的设计而改变。 尝试改变

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

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

看看这是否适合你。

显然,自从我的 rake routes 命令返回后,omniauth 路由的助手发生了变化

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

不像几个月前我开始这个项目时那样。

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

你也应该换行

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

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

这对我有用,希望对你有帮助。

【讨论】:

  • 成功了!非常感谢你,但现在我得到了一个不同的错误。用户中的 NoMethodError::OmniauthCallbacksController#facebook undefined method `provider' for nil:NilClass.
  • 您能发布一下 OmniauthCallbacksController 的 facebook 方法的样子吗?同时发布您的用户模型
  • 编辑帖子以提供控制器和模型。
  • 啊,我认为您的问题是您忘记在 request.env["omniauth"] 中添加“.auth”。 facebook 方法中获取用户的第一行应该是@user = User.from_omniauth(request.env["omniauth.auth"])。检查devise wiki 以使用omniauth-facebook 进行设置,特别是OmniauthCallbacksController#facebook 部分
  • 非常感谢。那工作得很好。我应该首先检查我的代码四次!
【解决方案2】:

这可能不是人们正在寻找的答案,但您始终可以在 Gemfile 中指定较旧版本的设计 gem。

gem 'devise', '~&gt; 4.1.1'

这导致我的生产站点瘫痪,我不得不迅速采取行动。这行得通。

【讨论】:

    猜你喜欢
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多