Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circular dependency detected while autoloading constant User

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
like image 983
Bejan George Avatar asked Aug 02 '13 09:08

Bejan George


Video Answer


2 Answers

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

like image 134
Jonathan Avatar answered Oct 19 '22 05:10

Jonathan


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

like image 24
ondrejbartas Avatar answered Oct 19 '22 03:10

ondrejbartas