Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Devise Ruby - webpage has a direct loop

Hi, I have an error about Devise. When I put before_filter :authenticate_user! in my dashboard controller or other controllers except sessions and registrations controller, I run localhost and it always has an error message: " this webpage has a direct loop". Can anyone help me this error? Thank you so much.

This is my routes.rb:

devise_for :users, :controllers => {:sessions => "sessions", :registrations => "registrations"}, :skip => [:passwords,:unlocks] 
  devise_scope :user do
    root to: "sessions#new"
    get '/confirm_code' => 'registrations#confirm_code'
    get '/register'     => 'registrations#new', as: :register
    get '/login'        => 'sessions#new', as: :login  
  end

This is my sessions controller:

class SessionsController < Devise::SessionsController
  layout 'sessions'
  def index
  end

  def new
    redirect_to '/dashboard'  if  user_signed_in?
  end

  def create
    login(params[:email],params[:password])
    if logged_in?
      if params[:remember_me] == '1'
        self.current_user.remember_me
        cookies[:auth_token] = { :value => self.current_user.remember_me_token , :expires => self.current_user.remember_me_token_expires_at }
      end
      redirect_to '/dashboard'
    else
      flash[:error] = 'The Username or Password you entered is incorrect.' # Not quite right!
      render 'new'
    end
  end

  def destroy
    self.current_user.forget_me if logged_in?
    cookies.delete :auth_token
    reset_session
    flash[:notice] = 'You have been logged out.'
    #redirect_back_or_default('/')
    redirect_to '/login'
  end
end

This is dashboard controller:

class DashboardController < ApplicationController
  before_filter :authenticate_user!  
  def index
  end
end

4 Answers

My sessions controller's code or all the redirects as you see actually are based on the ErpTechSVCS of CompassAE. Devise uses authenticate_user! in controllers to check users before they log in but ErpTechSVCS's code is already built with authenticate_user inside. So when my sessions controller's code is based on the ErpTechSVCS, it's duplicated. Few days ago, I removed everything about gem 'erp_tech_svcs', only used gem Devise, changed the code inside Sessions controller and it worked. Thanks for your reply, Jason. I'm appreciated that.

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Tuan Phan try changing your root url in the routes to something besides the new session page and see if that helps.

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

What happens if you take out all the redirects?

Hi Jason, I tried changing my root url to dashboard ( root :to => 'dashboard#index) but it still doesn't work (webpage has a direct loop). Do you think my sessions controller has too many redirect to '/dashboard'? I think maybe somewhere in sessions controller, I added too many redirect to '/dashboard' so it causes the problem.