How to stop the Rails debugger for the current request

后端 未结 9 1360
既然无缘
既然无缘 2021-02-02 15:50

Say I have a loop in my code that calls the rails debugger a few times

def show
    animals = [\'dog\', \'cat\', \'owl\', \'tiger\']
    for animal in animals
           


        
9条回答
  •  北恋
    北恋 (楼主)
    2021-02-02 16:30

    put your debugger statement somewhere before the iteration, then set a breakpoint inside the iteration which you can then clear later.

    Example:

    def index
    
      debugger
    
      @things = Thing.all
      @things.each do |thing|
        # ... something you want to check out in the debugger
        thing.some_calculation
      end
    end
    

    When you enter the debugger, set a breakpoint inside:

    b app/controllers/things_controller.rb:42
    

    (Where 42 is the line number you want to break at, like thing.some_calculation above. Note that it has to be an executable line of code -- comments, blank lines won't work). The debugger will show a breakpoint number and location:

    Breakpoint 1 at .../app/controllers/things_controller.rb:42
    

    Now, every time you continue, you will stop at the breakpoint. When you're done and want to complete the request, delete the breakpoint:

    delete 1
    

    continue once more, and you will complete the request!

提交回复
热议问题