找不到 Devise::OmniauthCallbacksController 的操作'facebook'(全身份验证的 Facebook 登录问题)



当点击我的RoR应用程序的"使用Facebook登录"按钮时,我收到错误:

AbstractController::ActionNotFound,位于/users/auth/facebook/recallback找不到Devise::OmniauthCallbacksController的操作"facebook">

你能看一下代码并提出尽可能多的解决方案吗?

以下是相关文件(询问是否需要更多(:

config/routes.rb

Rails.application.routes.draw do
devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" },
:controllers => { registrations: 'registrations' }
resources :posts
root "posts#index"
end

型号/用户.rb

class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:facebook]
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
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]
["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
has_many :posts
end

config/ininitializers/devert.rb

config.omniauth :facebook, 'my app id', 'my app secret'

controllers/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.from_omniauth(request.env["omniauth.auth"])
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
def failure
redirect_to root_path
end
end

控制器/post_controller.rb

class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@posts = Post.all.order("created_at DESC")
@post = Post.new
end
def show
end
def new
@post = current_user.posts.build
end
def edit
end
def create
@post = current_user.posts.build(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to root_path, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_post
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:post)
end
end

铁路路线添加

2: warning: key :controllers is duplicated and overwritten on line 3
Prefix Verb     URI Pattern                                                                              Controller#Action
new_user_session GET      /users/sign_in(.:format)                                                                 devise/sessions#new
user_session POST     /users/sign_in(.:format)                                                                 devise/sessions#create
destroy_user_session DELETE   /users/sign_out(.:format)                                                                devise/sessions#destroy
user_facebook_omniauth_authorize GET|POST /users/auth/facebook(.:format)                                                           devise/omniauth_callbacks#passthru
user_facebook_omniauth_callback GET|POST /users/auth/facebook/callback(.:format)                                                  devise/omniauth_callbacks#facebook
new_user_password GET      /users/password/new(.:format)                                                            devise/passwords#new
edit_user_password GET      /users/password/edit(.:format)                                                           devise/passwords#edit
user_password PATCH    /users/password(.:format)                                                                devise/passwords#update
PUT      /users/password(.:format)                                                                devise/passwords#update
POST     /users/password(.:format)                                                                devise/passwords#create
cancel_user_registration GET      /users/cancel(.:format)                                                                  registrations#cancel
new_user_registration GET      /users/sign_up(.:format)                                                                 registrations#new
edit_user_registration GET      /users/edit(.:format)                                                                    registrations#edit
user_registration PATCH    /users(.:format)                                                                         registrations#update
PUT      /users(.:format)                                                                         registrations#update
DELETE   /users(.:format)                                                                         registrations#destroy
POST     /users(.:format)                                                                         registrations#create
posts GET      /posts(.:format)                                                                         posts#index
POST     /posts(.:format)                                                                         posts#create
new_post GET      /posts/new(.:format)                                                                     posts#new
edit_post GET      /posts/:id/edit(.:format)                                                                posts#edit
post GET      /posts/:id(.:format)                                                                     posts#show
PATCH    /posts/:id(.:format)                                                                     posts#update
PUT      /posts/:id(.:format)                                                                     posts#update
DELETE   /posts/:id(.:format)                                                                     posts#destroy
root GET      /                                                                                        posts#index
rails_mandrill_inbound_emails POST     /rails/action_mailbox/mandrill/inbound_emails(.:format)                                  action_mailbox/ingresses/mandrill/inbound_emails#create
rails_postmark_inbound_emails POST     /rails/action_mailbox/postmark/inbound_emails(.:format)                                  action_mailbox/ingresses/postmark/inbound_emails#create
rails_relay_inbound_emails POST     /rails/action_mailbox/relay/inbound_emails(.:format)                                     action_mailbox/ingresses/relay/inbound_emails#create
rails_sendgrid_inbound_emails POST     /rails/action_mailbox/sendgrid/inbound_emails(.:format)                                  action_mailbox/ingresses/sendgrid/inbound_emails#create
rails_mailgun_inbound_emails POST     /rails/action_mailbox/mailgun/inbound_emails/mime(.:format)                              action_mailbox/ingresses/mailgun/inbound_emails#create
rails_conductor_inbound_emails GET      /rails/conductor/action_mailbox/inbound_emails(.:format)                                 rails/conductor/action_mailbox/inbound_emails#index
POST     /rails/conductor/action_mailbox/inbound_emails(.:format)                                 rails/conductor/action_mailbox/inbound_emails#create
new_rails_conductor_inbound_email GET      /rails/conductor/action_mailbox/inbound_emails/new(.:format)                             rails/conductor/action_mailbox/inbound_emails#new
edit_rails_conductor_inbound_email GET      /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format)                        rails/conductor/action_mailbox/inbound_emails#edit
rails_conductor_inbound_email GET      /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#show
PATCH    /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#update
PUT      /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#update
DELETE   /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#destroy
rails_conductor_inbound_email_reroute POST     /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format)                      rails/conductor/action_mailbox/reroutes#create
rails_service_blob GET      /rails/active_storage/blobs/:signed_id/*filename(.:format)                               active_storage/blobs#show
rails_blob_representation GET      /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
rails_disk_service GET      /rails/active_storage/disk/:encoded_key/*filename(.:format)                              active_storage/disk#show
update_rails_disk_service PUT      /rails/active_storage/disk/:encoded_token(.:format)                                      active_storage/disk#update
rails_direct_uploads POST     /rails/active_storage/direct_uploads(.:format)                                           active_storage/direct_uploads#create

看起来在路由中应该是users/omniauth_callbacks

devise_for :users, :controllers => { omniauth_callbacks: "users/omniauth_callbacks", registrations: 'registrations' }

希望能有所帮助!

最新更新