自动加载常量用户时检测到循环依赖性

我已经按照本教程( http://railscasts.com/episodes/236-omniauth-part-2 )使用OmniAuth和Devise创建facebook登录,我收到此错误:在我的路由中自动加载常量用户时检测到循环依赖性。 RB

devise_for :users , :controllers => {:registrations => 'registrations'} 

registrations_controller.rb

 Class RegistrationsController < Devise::RegistrationsController def create super session[:omniauth] = nil unless @user.new_record? end private def build_resource(*args) super if session["devise.omniauth"] @user.apply_omniauth(session["devise.omniauth"]) session["devise.omniauth"] = nil end end end 

这是来自AuthenticationsController的我的创建方法

 def create omniauth = request.env["omniauth.auth"] authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid']) if authentication flash[:notice] = "Signed in successfully." sign_in_and_redirect(:user, authentication.user) elsif current_user current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid']) flash[:notice] = "Authentication successful." redirect_to authentications_url else user = User.new user.apply_omniauth(omniauth) if user.save flash[:notice] = "Signed in successfully." sign_in_and_redirect(:user, user) else session[:omniauth] = omniauth.except('extra') redirect_to new_user_registration_url end end end 

您的registrations_controller.rb保存在哪里? 位置很重要。 我发现我在将它保存到app/controllers/devise/.犯了一个错误app/controllers/devise/. 。 它只需要保存在app/controllers/. 例如:

app/controllers/registrations_controller.rb


此外, config/routes.rb路由应定义为:

devise_for :users, controllers: { registrations: 'registrations' }

我在lib中的一些类中遇到了同样的问题(使用了config.autoload_paths += Dir["#{config.root}/lib/**/"]

对我来说帮助将rails从4.0.0.rc1切换到4.0.0

好吧,在我的development.rb中添加以下行后,我感到宽慰

 config.middleware.delete Rack::Lock 

参考: https : //github.com/websocket-rails/websocket-rails/issues/101

你最后可以尝试一次。

我有类似的问题。

然后意识到我在控制器内的不同文件夹中复制了相同的文件,这导致了问题。

我有两个文件具有相同的内容:

  app/controllers/logs_controller.rb app/controllers/api/logs_controller.rb 

很多gem开始在轨道4上打破,都是由于控制器中无法加载的问题。 https://github.com/flyerhzm/switch_user/issues/34 https://github.com/thoughtbot/high_voltage/issues/68 https://github.com/thoughtbot/clearance/issues/276等等

您应该查看gem正在创建问题的错误。 一旦你知道:1)检查该gem的开放问题2)如果该问题存在并修复,请确保您有该修复或更新gem。 3)如果没有,您可以创建一个问题并要求他们修复它。 4)如果你不想等待他们的修复,你可以形成gem并推送它的修复https://github.com/cashins/email_preview/commit/b34a077a954b98bd086153fae8979ad142667555所有修复是相同的(从指定的控制器删除卸载)

希望能帮助到你。

如果没有任何帮助降级您的rails版本。

我发现这在development.rb中有用:

 config.reload_classes_only_on_change = false 

(我之前尝试删除Gemfile.lock并运行bundle update,以及更改Rails版本,如此处和其他地方所述。它们对我不起作用。)

我用错字创建了同样的错误

 module EmployeeReminderssHelper 

何时调用辅助文件

 employee_reminders_helper.rb 

(注意额外的’s’)

设计维基就这个话题说了这么多:

参考: https : //github.com/plataformatec/devise/wiki/How-To : -redirect- to-a-specific-page-on-successful-sign-in#preventing-redirect- loops

防止重定向循环

因为上面的after_sign_in_path_for的代码只检查request.referer == sign_in_url,所以这些方法(调用after_sign_in_path_for)也必须被覆盖(否则你会遇到重定向循环):

 Devise::PasswordsController#after_resetting_password_path_for Devise::RegistrationsController#after_sign_up_path_for Devise::RegistrationsController#after_update_path_for 

这可以这样做:

 # routes.rb devise_for :users, controllers: { registrations: 'users/registrations', passwords: 'users/passwords' } # users/registrations_controller.rb class Users::RegistrationsController < Devise::RegistrationsController protected def after_sign_up_path_for(resource) signed_in_root_path(resource) end def after_update_path_for(resource) signed_in_root_path(resource) end end # users/passwords_controller.rb class Users::PasswordsController < Devise::PasswordsController protected def after_resetting_password_path_for(resource) signed_in_root_path(resource) end end