Devise + Google Auth +可安装引擎路由错误

时间:2014-10-13 08:07:04

标签: ruby-on-rails authentication ruby-on-rails-4 devise

我正在构建一个用于身份验证的rails应用程序。在这里,我使用rails引擎,其中包含整个身份验证过程,因为我正在使用googleauth with devise。 问题:当用户点击sign_in _with_google它成功登录后成功重定向gmail登录时,它会重定向到http://localhost:3000/users/auth/google_oauth2/callback?......这里显示的问题

Routing Error
uninitialized constant OmniauthCallbacksController

my_engine / routes.rb中

devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }, 
  :class_name => "MyEngine::User", module: :devise

omniauth_callbacks_controller.rb

require_dependency "my_engine/application_controller"

module MyEngine
  class OmniauthCallbacksController < Devise::OmniauthCallbacksController   
    def googleoauth2
      .....
    end
  end
end

login.html.erb

<%= link_to "Sign in with Google", my_engine.user_omniauth_authorize_path(:google_oauth2) %>

如果需要任何信息,请发表评论我将提前致谢

development.log

Started GET "/" for 127.0.0.1 at 2014-10-13 18:25:20 +0530
  [1m[36mActiveRecord::SchemaMigration Load (21.0ms)[0m  [1mSELECT `schema_migrations`.* FROM `schema_migrations`[0m
Processing by MyEngine::DashboardController#index as HTML
Redirected to http://localhost:3000/test/home
Completed 302 Found in 62ms (ActiveRecord: 0.0ms)


Started GET "/" for 127.0.0.1 at 2014-10-13 18:25:21 +0530
Processing by MyEngine::DashboardController#index as HTML
Redirected to http://localhost:3000/test/home
Completed 302 Found in 1ms (ActiveRecord: 0.0ms)


Started GET "/test/home" for 127.0.0.1 at 2014-10-13 18:25:21 +0530
Processing by TestController#home as HTML
Completed 401 Unauthorized in 26ms


Started GET "/users/sign_in" for 127.0.0.1 at 2014-10-13 18:25:21 +0530
Processing by Devise::SessionsController#new as HTML
  Rendered /Users/user/Documents/NewApps/Rails4.0.10/fd/MyEngine/app/views/devise/sessions/new.html.erb within layouts/my_engine/application (32.4ms)
Completed 200 OK in 245ms (Views: 178.1ms | ActiveRecord: 46.9ms)


Started GET "/assets/my_engine/dashboard.css?body=1" for 127.0.0.1 at 2014-10-13 18:25:22 +0530


Started GET "/assets/my_engine/omniauth_callbacks.css?body=1" for 127.0.0.1 at 2014-10-13 18:25:22 +0530


Started GET "/assets/my_engine/application.css?body=1" for 127.0.0.1 at 2014-10-13 18:25:22 +0530


Started GET "/assets/my_engine/engine_layout.css?body=1" for 127.0.0.1 at 2014-10-13 18:25:22 +0530


Started GET "/assets/my_engine/application.js?body=1" for 127.0.0.1 at 2014-10-13 18:25:22 +0530


Started GET "/assets/my_engine/dashboard.js?body=1" for 127.0.0.1 at 2014-10-13 18:25:22 +0530


Started GET "/assets/my_engine/omniauth_callbacks.js?body=1" for 127.0.0.1 at 2014-10-13 18:25:22 +0530


Started GET "/assets/my_engine/logo.png" for 127.0.0.1 at 2014-10-13 18:25:22 +0530


Started GET "/assets/my_engine/btn_red_32.png" for 127.0.0.1 at 2014-10-13 18:25:22 +0530


Started GET "/users/auth/google_oauth2" for 127.0.0.1 at 2014-10-13 18:25:26 +0530


Started GET "/users/auth/google_oauth2/callback?state=4081132f9229erea9bb19b9174cce33fa7asdf5c3f708f96e1862020&code=4/Fd9y3m28CTrX_L1a9yeBMAsdcfer0V05rMv6UdfUmE23vfw8sdfyxI.4jR45xWflI4Tcp7tdiljKKbtbisTkgI" for 127.0.0.1 at 2014-10-13 18:25:27 +0530

ActionController::RoutingError (uninitialized constant OmniauthCallbacksController):
  activesupport (4.1.6) lib/active_support/inflector/methods.rb:238:in `const_get'
  activesupport (4.1.6) lib/active_support/inflector/methods.rb:238:in `block in constantize'
  activesupport (4.1.6) lib/active_support/inflector/methods.rb:236:in `each'
  activesupport (4.1.6) lib/active_support/inflector/methods.rb:236:in `inject'
  activesupport (4.1.6) lib/active_support/inflector/methods.rb:236:in `constantize'
  actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:78:in `controller_reference'
  actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:68:in `controller'
  actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:46:in `call'
  actionpack (4.1.6) lib/action_dispatch/routing/mapper.rb:45:in `call'
  actionpack (4.1.6) lib/action_dispatch/journey/router.rb:73:in `block in call'
  actionpack (4.1.6) lib/action_dispatch/journey/router.rb:59:in `each'
  actionpack (4.1.6) lib/action_dispatch/journey/router.rb:59:in `call'
  actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:678:in `call'
  railties (4.1.6) lib/rails/engine.rb:514:in `call'
  railties (4.1.6) lib/rails/railtie.rb:194:in `public_send'
  railties (4.1.6) lib/rails/railtie.rb:194:in `method_missing'
  actionpack (4.1.6) lib/action_dispatch/journey/router.rb:73:in `block in call'
  actionpack (4.1.6) lib/action_dispatch/journey/router.rb:59:in `each'
  actionpack (4.1.6) lib/action_dispatch/journey/router.rb:59:in `call'
  actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:678:in `call'
  omniauth (1.2.2) lib/omniauth/strategy.rb:404:in `call_app!'
  omniauth (1.2.2) lib/omniauth/strategy.rb:362:in `callback_phase'
  omniauth-oauth2 (1.2.0) lib/omniauth/strategies/oauth2.rb:77:in `callback_phase'
  omniauth (1.2.2) lib/omniauth/strategy.rb:227:in `callback_call'
  omniauth (1.2.2) lib/omniauth/strategy.rb:184:in `call!'
  omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
  warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.3) lib/warden/manager.rb:34:in `catch'
  warden (1.2.3) lib/warden/manager.rb:34:in `call'
  rack (1.5.2) lib/rack/etag.rb:23:in `call'
  rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
  rack (1.5.2) lib/rack/head.rb:11:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/flash.rb:254:in `call'
  rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.1.6) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
  activerecord (4.1.6) lib/active_record/migration.rb:380:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.1.6) lib/active_support/callbacks.rb:82:in `run_callbacks'
  actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.6) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.6) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.6) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.6) lib/rails/engine.rb:514:in `call'
  railties (4.1.6) lib/rails/application.rb:144:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  /Users/user/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /Users/user/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /Users/user/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'


  Rendered /Users/user/.rvm/gems/ruby-1.9.3-p547@rails4.1.6/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.2ms)
  Rendered /Users/user/.rvm/gems/ruby-1.9.3-p547@rails4.1.6/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_route.html.erb (0.8ms)
  Rendered /Users/user/.rvm/gems/ruby-1.9.3-p547@rails4.1.6/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_route.html.erb (2.2ms)
  Rendered /Users/user/.rvm/gems/ruby-1.9.3-p547@rails4.1.6/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_table.html.erb (41.3ms)
  Rendered /Users/user/.rvm/gems/ruby-1.9.3-p547@rails4.1.6/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (93.8ms)

omniauth_callbacks_controller.rb

 def googleoauth2  
  @auth = env[OMNI_AUTH]
  @auth_user = AuthUsers.find_by_email(@auth.info.email)
  unless @auth_user
    flash[:error] = "Sorry, you are not authorized."
    redirect_to "/users/sign_in"
  end
  @signed_up_user = User.find_by_email(@auth.info.email)
  email = @auth.info.email
  unless @signed_up_user
    @user = User.find_for_google_oauth2(request.env[OMNI_AUTH], current_user)
  end
  if @auth_user && @signed_up_user
    @user = User.find_for_google_oauth2(request.env[OMNI_AUTH], current_user)
    sign_in_and_redirect @user, :event => :authentication
  end
end

目录结构::

MyEngine ->app -> controllers -> my_engine -> omniauth_callbacks_controller.rb

1 个答案:

答案 0 :(得分:1)

my_engine / routes.rb中的更改

devise_for:users,:controllers =&gt; {:omniauth_callbacks =&gt; &#34; omniauth_callbacks&#34; },   :class_name =&gt; &#34; MyEngine :: User&#34;,module :: devise

devise_for:users,:controllers =&gt; {:omniauth_callbacks =&gt; &#34; my_engine / omniauth_callbacks&#34; },   :class_name =&gt; &#34; MyEngine :: User&#34;,module :: devise