【问题标题】:Routes not found in rails在 rails 中找不到路线
【发布时间】:2015-12-19 11:25:37
【问题描述】:

我有 2 种方式登录我的应用程序。第一种方式是通过电子邮件登录,下一种是通过社交媒体登录。

我已经安装了 devise 和 omniauth、omniauth-facebook、twitter 和 google plus。我已经通过 devise 成功实现了正常登录,但是当我尝试通过访问 /auth/facebook 通过 fb 或 twitter 或 gplus 登录时,它说找不到路由。

    Rails.application.routes.draw do
  devise_for :users
  root to: 'visitors#index'
  get '/auth/:provider/callback' => 'sessions#create'
  get '/signin' => 'sessions#new', as: :signin
  get '/signout' => 'sessions#destroy', as: :signout
  get '/auth/failure' => 'sessions#failure'
end

//session_controller.rb

    class SessionsController < ApplicationController
  def new
    redirect_to '/auth/facebook'
  end

  def create
    auth = request.env['omniauth.auth']
    user = User.where(provider: auth['provider'],
                      uid: auth['uid'].to_s).first || User.create_with_omniauth(auth)
    reset_session
    session[:user_id] = user.id
    redirect_to root_url, notice: 'Signed in!'
  end

  def destroy
    reset_session
    redirect_to root_url, notice: 'Signed out!'
  end

  def failure
    redirect_to root_url, alert: "Authentication error: #{params[:message].humanize}"
  end
end

//omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'],
           scope: 'public_profile', info_fields: 'id,name,link'
  provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']
  provider :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_SECRET'],
           scope: 'profile', image_aspect_ratio: 'square', image_size: 48,
           access_type: 'online', name: 'google'
end

【问题讨论】:

    标签: ruby-on-rails devise omniauth


    【解决方案1】:

    取消注释

    devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :confirmable, :lockable, :timeoutable //, :omniauthable
    

    在 user.rb 中成功了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-05
      • 2017-10-10
      • 2022-01-04
      • 2018-03-03
      • 1970-01-01
      • 2010-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多