ActionController :: RoutingError(未初始化的常量Users :: OmniauthCallbacksController)Devise和google_oauth2

问题描述 投票:0回答:1

我已经按照this link的教程进行操作,最后得到以下日志消息:

Started GET "/users/auth/google_oauth2" for 127.0.0.1 at 2019-02-22 20:59:25 +1100
I, [2019-02-22T20:59:25.512091 #11001]  INFO -- omniauth: (google_oauth2) Request phase initiated.
Started GET "/users/auth/google_oauth2/callback?state=...
I, [2019-02-22T20:59:29.060352 #11001]  INFO -- omniauth: (google_oauth2) Callback phase initiated.

ActionController::RoutingError (uninitialized constant Users::OmniauthCallbacksController):

我在网上搜索过,所有解决方案建议检查各种文件中的拼写。我把它们包括在下面。

设计_links.html.erb:

<%- if devise_mapping.omniauthable? %>
  <%- resource_class.omniauth_providers.each do |provider| %>
    <%= link_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", user_google_oauth2_omniauth_authorize_path %><br />
  <% end -%>
<% end -%>

devise.rb:

config.omniauth :google_oauth2, client_id, client_secret, {
    scope: "contacts.readonly,userinfo.email,userinfo.profile,youtube",
    prompt: 'select_account',
    image_aspect_ratio: 'square',
    image_size: 50
}

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 => [:google_oauth2]

  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.token = auth.credentials.token
      user.expires = auth.credentials.expires
      user.expires_at = auth.credentials.expires_at
      user.refresh_token = auth.credentials.refresh_token
    end
  end
end

/app/controllers/users/OM你AU TH_callbacks_controllers.日本

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

  def google_oauth2
    @user = User.from_omniauth(request.env["omniauth.auth"])
    if @user.persisted?
      sign_in @user, :event => :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, :kind => "Google") if is_navigational_format?
    else
      session["devise.google_data"] = request.env["omniauth.auth"]
    end
    redirect_to '/'
  end

end

的routes.rb

devise_for :users, controllers: {omniauth_callbacks: "users/omniauth_callbacks"}

谷歌控制台

我使用以下授权重定向网址设置了我的Google控制台:

http://localhost:3000/users/auth/google_oauth2/callback

Rails路由

当我做rails routes我有:

user_google_oauth2_omniauth_authorize GET|POST /users/auth/google_oauth2(.:format)                                                      users/omniauth_callbacks#passthru
user_google_oauth2_omniauth_callback GET|POST /users/auth/google_oauth2/callback(.:format)                                             users/omniauth_callbacks#google_oauth2

任何帮助,知道为什么这不起作用将不胜感激。

ruby-on-rails devise omniauth omniauth-google-oauth2
1个回答
1
投票

/app/controllers/users/OM你AU TH_callbacks_controllers.日本

这是不正确的。您的控制器名称中还有一个额外的s。这就是rails无法找到该类的原因。您应该将控制器名称重命名为omniauth_callbacks_controller.rb

© www.soinside.com 2019 - 2024. All rights reserved.