以下のページを参考に実装を行いました。

[gem omniauth-google-oauth2 で認証させる -
Qiita](https://qiita.com/bino98/items/596b5cffeca7c104bd90)

その後いくつかのバージョン違いによるエラーは取り除けたのですが、googleの認証画面からリダイレクトしたときに以下のエラーが出てしまいます。

**エラーメッセージ**

```

    uninitialized constant Users::OmniauthCallbacksController
    
```

この問題の対処方法がわかる方教えてください。

* * *

Gemfile

omniauth_callbacks_controller.rb  

```

    source 'https://rubygems.org'
    git_source(:github) { |repo| "https://github.com/#{repo}.git" }
    
    ruby '2.7.0'
    
    # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
    gem 'rails', '~> 6.0.2', '>= 6.0.2.1'
    # Use sqlite3 as the database for Active Record
    gem 'sqlite3', '~> 1.4'
    # Use Puma as the app server
    gem 'puma', '~> 4.1'
    # Use SCSS for stylesheets
    gem 'sass-rails', '>= 6'
    # Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
    gem 'webpacker', '~> 4.0'
    # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
    gem 'turbolinks', '~> 5'
    # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
    gem 'jbuilder', '~> 2.7'
    # Use Redis adapter to run Action Cable in production
    # gem 'redis', '~> 4.0'
    # Use Active Model has_secure_password
    # gem 'bcrypt', '~> 3.1.7'
    
    # Use Active Storage variant
    # gem 'image_processing', '~> 1.2'
    
    # Reduces boot times through caching; required in config/boot.rb
    gem 'bootsnap', '>= 1.4.2', require: false
    
    group :development, :test do
      # Call 'byebug' anywhere in the code to stop execution and get a debugger console
      gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
    end
    
    group :development do
      # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
      gem 'web-console', '>= 3.3.0'
      gem 'listen', '>= 3.0.5', '< 3.2'
      # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
      gem 'spring'
      gem 'spring-watcher-listen', '~> 2.0.0'
    end
    
    group :test do
      # Adds support for Capybara system testing and selenium driver
      gem 'capybara', '>= 2.15'
      gem 'selenium-webdriver'
      # Easy installation and use of web drivers to run system tests with browsers
      gem 'webdrivers'
    end
    
    # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
    gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
    gem 'devise'
    gem 'omniauth'
    gem 'omniauth-google-oauth2'
    
```

```

    class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
       def google
         @user = User.find_for_google(request.env['omniauth.auth'])
    
         if @user.persisted?
           flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Google'
           sign_in_and_redirect @user, event: :authentication
         else
           session['devise.google_data'] = request.env['omniauth.auth']
           redirect_to new_user_registration_url
         end
      end
    end
    
```

user.rb

```

    class User < ApplicationRecord
      # Include default devise modules. Others available are:
      # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :validatable,:trackable, :omniauthable, omniauth_providers: %i(google)
    
      # protected
      def self.find_for_google(auth)
        user = User.find_by(email: auth.info.email)
    
        unless user
          user = User.create(name:     auth.info.name,
                             provider: auth.provider,
                             uid:      auth.uid,
                             token:    auth.credentials.token,
                             password: Devise.friendly_token[0, 20],
                             meta:     auth.to_yaml)
        end
        user
      end
    end
    
```

devise.rb（追加分）

```

      Devise.setup do |config|
      require 'devise/orm/active_record'
      config.omniauth :google_oauth2,
                      'CLIENT-ID', # 実際はgoogleからとってきたIDが入ります
                      'SERCRET-ID', # 同様
                      skip_jwt: true ,
                      name: :google,
                      scope: %w(email) 
    end
    
```

routes.rb

```

    Rails.application.routes.draw do
      get 'home/index'
      root to: "home#index"
      # devise_for :users
        devise_for :users, controllers: {
          omniauth_callbacks: "users/omniauth_callbacks"
      }
      # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
    end
    
```
CC BY-SA 4.0
2020-02-20T13:04:05.830
63210
2023-06-21T01:08:46.080
2020-02-20T13:11:07.263
3060
19263
question
ruby-on-rails
rails6.0 + omniauth-google-oauth2 で認証を行うとuninitialized constantがおきる
すでにコメントにもありますが、Railsは定数（クラスを含みます）を自動読み込みする際にファイル名とディレクトリ構造が一致している必要があります（参考：<https://railsguides.jp/autoloading_and_reloading_constants.html#%E3%83%97%E3%83%AD%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E3%81%AE%E6%A7%8B%E9%80%A0>）

>
> app/controllers/admin/payments_controller.rbではAdmin::PaymentsControllerを定義すべき

このように、`Users::OmniauthCallbacksController`は`app/controllers/users/omniauth_callbacks_controller.rb`に置かれていないと正しく自動読み込みされません。
CC BY-SA 4.0
2020-03-07T04:44:18.453
63641
2020-03-07T04:44:18.453
442
63210
answer