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

时间:2019-02-22 10:15:28

标签: ruby-on-rails devise omniauth omniauth-google-oauth2

我已经按照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):

我已经在线搜索了,所有解决方案都建议检查各种文件中的拼写。我将它们包括在下面。

  

Devise _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/omniauth_callbacks_controllers.rb

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控制台

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

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

铁路路线

当我做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

非常感谢您提供任何帮助,以了解为什么此方法不起作用。

1 个答案:

答案 0 :(得分:1)

  

/app/controllers/users/omniauth_callbacks_controllers.rb

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