Не удалось найти действие «facebook» для Devise :: OmniauthCallbacksController (проблема входа в Facebook с помощью omniauth) - PullRequest
0 голосов
/ 24 октября 2019

при нажатии кнопки «Войти через Facebook» в моем приложении RoR я получаю сообщение об ошибке:

AbstractController :: ActionNotFound в / users / auth / facebook / callback The action 'facebook'не может быть найдено для Devise :: OmniauthCallbacksController

Можете ли вы взглянуть на код и предложить столько решений, сколько сможете?

здесь есть файлы (спросите,хочу больше):

config / rout.rb :

Rails.application.routes.draw do
  devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" },
  :controllers => { registrations: 'registrations' }

  resources :posts

  root "posts#index"
end

models / user.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 / initializers / devise.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

controllers / posts_controller. рб

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

1 Ответ

0 голосов
/ 24 октября 2019

Похоже, на маршрутах это должно быть users/omniauth_callbacks

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

Надеюсь, это поможет!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...