加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Devise Omniauth:未定义的方法`user_omniauth

发布时间:2020-12-16 22:45:04 所属栏目:百科 来源:网络整理
导读:我一直在追踪 omniauth + devise integration guide for facebook,当我点击链接使用Facebook链接登录时,我会收到这个错误. 未定义的方法user_omniauth_authorize_path for## Class:0x0000000253a550:0x000000035ea490 我已经仔细检查了指南中的代码,我似乎
我一直在追踪 omniauth + devise integration guide for facebook,当我点击链接使用Facebook链接登录时,我会收到这个错误.

未定义的方法user_omniauth_authorize_path for#<#< 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,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

解决方法

尝试运行耙子路线,看看omniauth的路径.我相信他们已经改变了最新版本的设计.
尝试改变
<%= link_to "Sign in with Facebook",user_omniauth_authorize_path(:facebook) %>

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

看看是否适合你.

显然,由于我返回的耙路指挥,全能路线的帮手已经改变了

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"])

这对我有用,希望这有帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读