11

I get this error about my routes file

SystemStackError (stack level too deep):
  actionpack (3.2.8) lib/action_dispatch/middleware/reloader.rb:70


  Rendered /Users/duy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.5ms)
  Rendered /Users/duy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.3ms)
  Rendered /Users/duy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (19.9ms)

I could isolate the problematic code but don't understand what's creating the infinite loop:

  devise_for :users, :controllers => { :registrations => "registrations", :sessions => "sessions", :omniauth_callbacks => "users/omniauth_callbacks" }
  devise_scope :user do
    match '/sessions/simulate_user/:id' => 'sessions#simulate_user', :as => :simulate_user_sessions
    match '/sessions/leave_simulation_mode' => 'sessions#leave_simulation_mode', :as => :leave_simulation_mode_sessions
    get "user_confirmation", :to => "devise/confirmations#create"
    get "after_confirmation", :to => "challenges#index"
  end

Any help would be greatly appreciated!

EDIT:

class SessionsController < Devise::SessionsController

  def new

    @after_sign_in_page = params[:after_sign_in_page] if params[:after_sign_in_page]
    super
  end

  def create
    params[:user][:email].downcase!
    super
  end

  def simulate_user
    if can? :simulate_user, User
      admin = current_user.id
      sign_out
      @user = User.find(params[:id])
      if sign_in @user
        session[:simulation_for] = admin
        redirect_to request.referer
      else
        flash[:notice] = "Something went wrong"
        redirect_to action: "leave_simulation_mode"
      end
    end
  end

  def leave_simulation_mode
    @user = User.find(session[:simulation_for])
    sign_out
    if sign_in @user
      session[:simulation_for] = nil
    else
      flash[:notice] = "something went wrong..."
    end
    redirect_to request.referer
  end

end
class ChallengesController < ApplicationController
      def index
        params[:format] = "html" unless params[:subaction].nil?
        @video = true unless user_signed_in?
        if current_user
          @current_page = params[:page] ? params[:page].to_i : 1
          @columns = params[:cols] ? params[:cols] : 2
          @keyword = params[:keyword]
          @search = true if params[:search] == "1"
          @challenges = Challenge.is_open.where(id: current_user.challenges.filtered(params[:keyword],params[:user],params[:expertize]).collect(&:root_id).uniq)
          logger.debug "Challenges found: #{@challenges.count}"
          @users = User.scoped
          @expertizes = Expertize.all
        end

        respond_to do |format|
          format.html {
            if current_user
              if current_user.challenges.waiting_for_approval.count == 1 and @challenges.count == 1
                redirect_to challenge_path(current_user.challenges.waiting_for_approval.first, subaction: "description")
                return
                logger.debug "REdirect called"
              else
                @challenges = @challenges.page(1).per(@current_page.to_i * 10).order('children_count+tasks_count desc')
              end
            end
            render
            }
          format.json { render json: @challenges }
          format.js {
            @challenges = @challenges.page(@current_page).per(10).order('children_count+tasks_count desc')
            render
          }
        end
      end
end

Also, I forgot to mention that when I restart the local server, it works fine. Every time I change my routes.rb file, I get this error. Then I have to restart the Thin server, then I can work further on...

EDIT 2:

rake route output: https://www.dropbox.com/s/knmkk1f54vx47yj/rake%20routes.rtf

ndemoreau
  • 3,849
  • 4
  • 43
  • 55
  • Can you add the simulate_user, leave_simulation_mode challenges#index methods? You can also have some error in your sessions and registrations controllers. Are you overriding all devise methods in these controllers? – Andreas Lyngstad Dec 17 '12 at 19:21
  • look like you have unneeded `end` before `def index` – Tolik Kukul Dec 18 '12 at 07:56
  • I don't think so. The last end closes the sessionsController. – ndemoreau Dec 18 '12 at 08:12
  • could you run `rake routes` and tell us the output ? – Khaled Dec 31 '12 at 10:02
  • Done. In attached file because was too long to be added – ndemoreau Dec 31 '12 at 10:40
  • Is there a reason `get "after_confirmation", :to => "challenges#index"` is in your `devise_scope` when `ChallengesController` doesn't inherit from a Devise controller? – Alex Ghiculescu Jan 01 '13 at 15:08
  • No, I followed the Devise directives and, as I didn't go deep into Devise, I don't really know the reason... But anyway, taking it out of the Devise scope doesn't solve the issue. – ndemoreau Jan 02 '13 at 07:25
  • Can you post the full backtrace? If you are seeing it in the web UI, you can click in "Full Backtrace". If you are seeing it just in console, there is a config/initializers/backtrace_silencers.rb in your app, just uncomment the last line and reproduce the issue again. – José Valim Jan 02 '13 at 07:42
  • This is actually a full backtrace. I just tried it by removing the silencer but I get the same. – ndemoreau Jan 02 '13 at 08:09
  • Updating to devise 2.2.1 solved the issue for me! – wdspkr Jan 15 '13 at 12:49

4 Answers4

5

I have a same problem with you. This must be a issue caused by some gems.

Like I use "devise_invitable" with "devise". then every time I modified route file in development environment. it will raise an "stack too deep" error from "lib/action_dispatch/middleware/reloader.rb".

After reading your post. I try to comment devise_for :users. Reload routes is ok.

Then I try to remove "devise_invitable" from Gemfile.

After made changes. it doesn't raise error. This happend after I use Rails 3.2.9. I have another project < Rails 3.2.9, two gems works correct.

Ankun
  • 434
  • 2
  • 10
  • Hey Blade, thanks for your very helpful answer. It brought me to search in the devise_invitable issues on Github and it appeared that this is is known and linked to the versions post 1.1.2 of the gem. I did the rollback and I don't have the issue anymore. Have a great day! – ndemoreau Jan 11 '13 at 08:37
  • This has since been fixed in the devise_invitable gem. See my answer for details. – Liron Yahdav Jan 31 '13 at 22:52
2

I'm only guessing, but your construct of SessionsController#simulate_user and SessionsController#leave_simulation_mode exposes an infinite redirection loop.

If you access the url /simulate_user and the sign in doesn't work (else path) you redirect to /leave_simulation_mode. Now in leave_simulation_mode, no matter if the sign out/sign in works or not you redirect back to /simulate_user again, due to redirect_to request.referer.

Now back in /simulate_user, the user is signed in again, failing again --> redirecting again.

If something's blocking your simulated user from being signed in correctly, you got an infinite redirection loop:

simulate_user -> leave_simulation_mode -> simulate_user -> leave_simulation_mode --> ...

Hope that helps!

Vapire
  • 4,568
  • 3
  • 24
  • 41
  • If I remove the two methods from the code, I still have the issue. It's possible that I have a problem with these methods but I don't think so. I've been using them for months without any problem. But anyway, this "stack level too deep" issue is not linked to these methods. – ndemoreau Dec 31 '12 at 11:16
  • Hmm, ok... Did you make sure that your user object CAN actually sign in? – Vapire Jan 01 '13 at 18:49
  • Yes, sure. You know, when I get this stack level too deep. I restart my local server and everything works fine again. I only get the message when I change something in my routes file – ndemoreau Jan 02 '13 at 07:23
1

Move all monkeypatches to initalizers.

I have the same problem due to monkeypatching of ActionDispatch::Request. Patch was placed to base controller. When I replace it to initializers, issue was fixed.

gayavat
  • 18,910
  • 11
  • 45
  • 55
0

It's not clear from the other answers, but this was a bug in devise_invitable gem that was fixed in version 1.1.5. Here's the issue on GitHub: https://github.com/scambra/devise_invitable/issues/252

Liron Yahdav
  • 10,152
  • 8
  • 68
  • 104