Ruby-on-rails 自动加载常量用户时检测到循环依赖
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18013055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Circular dependency detected while autoloading constant User
提问by Bejan George
I've followed this tutorial (http://railscasts.com/episodes/236-omniauth-part-2) for creating facebook login with OmniAuth and Devise and I get this error: Circular dependency detected while autoloading constant User in my routes.rb
我已经按照本教程 ( http://railscasts.com/episodes/236-omniauth-part-2) 使用 OmniAuth 和 Devise 创建 facebook 登录,我收到此错误:在我的路线中自动加载常量用户时检测到循环依赖。 RB
devise_for :users , :controllers => {:registrations => 'registrations'}
registrations_controller.rb
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
and here's my create method from AuthenticationsController
这是我来自 AuthenticationsController 的 create 方法
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
回答by Jonathan
Where was your registrations_controller.rbsaved to? The location is important. I found that I was making a mistake saving it to app/controllers/devise/.. It simply needed to be saved in app/controllers/.e.g.:
你registrations_controller.rb被救到哪里去了?位置很重要。我发现我在将它保存到app/controllers/devise/.. 它只需要保存在app/controllers/.例如:
app/controllers/registrations_controller.rb
app/controllers/registrations_controller.rb
Also, config/routes.rbroute should be defined as:
此外,config/routes.rb路由应定义为:
devise_for :users, controllers: { registrations: 'registrations' }
devise_for :users, controllers: { registrations: 'registrations' }
回答by Daniel Pérez Rada
I had a similar problem.
我有一个类似的问题。
And then realised I have the same file duplicated in different folders inside controller, and that was causing the problem.
然后意识到我在控制器内的不同文件夹中复制了相同的文件,这导致了问题。
I had both files with the same content:
我有两个内容相同的文件:
app/controllers/logs_controller.rb
app/controllers/api/logs_controller.rb
回答by ondrejbartas
I got same problem with some classes in lib (used config.autoload_paths += Dir["#{config.root}/lib/**/"])
我在 lib (used config.autoload_paths += Dir["#{config.root}/lib/**/"]) 中的某些类遇到了同样的问题
for me helped to switch rails from 4.0.0.rc1to 4.0.0
对我来说帮助从道岔尖轨4.0.0.rc1至4.0.0
回答by Rubyrider
Well, I got relief after adding the following line in my development.rb
好吧,在我的 development.rb 中添加以下行后我松了一口气
config.middleware.delete Rack::Lock
Reference: https://github.com/websocket-rails/websocket-rails/issues/101
参考:https: //github.com/websocket-rails/websocket-rails/issues/101
You can try this once at last.
你可以试试这个最后一次。
回答by mahemoff
I found this works in development.rb:
我发现这在 development.rb 中有效:
config.reload_classes_only_on_change = false
(I previously tried deleting Gemfile.lock and running bundle update, as well as changing Rails version, as mentioned here and elsewhere. They didn't work for me.)
(我之前尝试删除 Gemfile.lock 并运行 bundle update,以及更改 Rails 版本,如此处和其他地方所述。它们对我不起作用。)
回答by Sahil Dhankhar
a lot of gems started breaking on rails 4 , all due to the problem of unloadable in controllers. https://github.com/flyerhzm/switch_user/issues/34https://github.com/thoughtbot/high_voltage/issues/68https://github.com/thoughtbot/clearance/issues/276and many more
许多 gems 开始在 rails 4 上损坏,这都是由于控制器中的可卸载问题。 https://github.com/flyerhzm/switch_user/issues/34 https://github.com/thoughtbot/high_voltage/issues/68 https://github.com/thoughtbot/clearance/issues/276等等
you should look into errors that which gem is creating the problem. Once you know that: 1) Check the open issues of that gem 2) If that issue exists and fixed , make sure you have that fix or else update the gem. 3)If not you can create an issue and ask them to fix it. 4) If you dont want to wait for their fix , you can form the gem and push a fix for it https://github.com/cashins/email_preview/commit/b34a077a954b98bd086153fae8979ad142667555all fix are the same(removing unloadable from the specified controller )
您应该查看导致问题的 gem 的错误。一旦您知道:1) 检查该 gem 的未解决问题 2) 如果该问题存在并已修复,请确保您已修复或更新 gem。3)如果没有,您可以创建一个问题并要求他们修复它。4)如果你不想等待他们的修复,你可以形成 gem 并为它推送修复 https://github.com/cashins/email_preview/commit/b34a077a954b98bd086153fae8979ad142667555所有修复都是相同的(从指定的控制器中卸载)
Hope it helps.
希望能帮助到你。
if nothing helps downgrade your rails version.
如果没有任何帮助降级您的 rails 版本。
回答by jared
I created the same error with a typo, I had
我用错字创建了同样的错误,我有
module EmployeeReminderssHelper
when the helper file was called
调用帮助文件时
employee_reminders_helper.rb
(Note the extra 's')
(注意额外的's')
回答by Evolve
The devise wiki had this to say on the topic:
设计维基对这个话题有这样的说法:
参考:https: //github.com/plataformatec/devise/wiki/How-To: -redirect-to-a-specific-page-on-successful-sign-in#preventing-redirect- loops
Preventing redirect loops
防止重定向循环
Because the code for after_sign_in_path_for above only checks if request.referer == sign_in_url, these methods (which call after_sign_in_path_for) will also have to be overridden (else you will encounter 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
This can be done like so:
这可以像这样完成:
# 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

