Circular dependency detected while autoloading constant User

五迷三道 提交于 2019-11-26 21:47:25

问题


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

  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

and here's my create method from 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

回答1:


Where was your registrations_controller.rb saved 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.:

app/controllers/registrations_controller.rb


Also, config/routes.rb route should be defined as:

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




回答2:


I got same problem with some classes in lib (used config.autoload_paths += Dir["#{config.root}/lib/**/"])

for me helped to switch rails from 4.0.0.rc1 to 4.0.0




回答3:


Well, I got relief after adding the following line in my development.rb

config.middleware.delete Rack::Lock

Reference: https://github.com/websocket-rails/websocket-rails/issues/101

You can try this once at last.




回答4:


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



回答5:


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/34 https://github.com/thoughtbot/high_voltage/issues/68 https://github.com/thoughtbot/clearance/issues/276 and many more

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/b34a077a954b98bd086153fae8979ad142667555 all fix are the same(removing unloadable from the specified controller )

Hope it helps.

if nothing helps downgrade your rails version.




回答6:


I found this works in 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.)




回答7:


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')




回答8:


The devise wiki had this to say on the topic:

Ref: 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):

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


来源:https://stackoverflow.com/questions/18013055/circular-dependency-detected-while-autoloading-constant-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!