13

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
Bejan George
  • 176
  • 1
  • 1
  • 6

8 Answers8

12

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

Jonathan
  • 10,936
  • 8
  • 64
  • 79
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

2

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.

Rubyrider
  • 3,567
  • 1
  • 28
  • 34
2

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
Daniel Pérez Rada
  • 1,441
  • 1
  • 13
  • 8
0

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.

Sahil Dhankhar
  • 3,596
  • 2
  • 31
  • 44
0

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

mahemoff
  • 44,526
  • 36
  • 160
  • 222
0

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

jared
  • 231
  • 3
  • 13
-1

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
Evolve
  • 8,939
  • 12
  • 51
  • 63
  • The specific error the OP quoted is a Rails error pertaining to auto-loading, not a browser error pertaining to redirect loops. – Nathan Dec 05 '14 at 01:10
  • I found the OPs post and came here seeking answers, after hunting down a fix myself I circled back here and posted the answer that worked for me the insights being that the 'circular dependancy' mentioned in the error message was related to the way Devise was auto redirecting. – Evolve Dec 11 '14 at 05:30