What are some tips for debugging Ruby erb files?

Currently, when I get an error in the erb template (for use with HTTPServer / cgi), I do the following:

  • If it's a small change, check back, save and check again.
  • For large change or new file, delete or comment code 1/2 and check again. Do a binary search until I remove / find the broken code.

The call stack doesn't match anything in my .rhtml file.

(erb):6:in `block in <main>'
/opt/local/lib/ruby/1.9.1/erb.rb:753:in `eval'
/opt/local/lib/ruby/1.9.1/erb.rb:753:in `result'
bin/correct.rb:45:in `block in <main>'
/opt/local/lib/ruby/1.9.1/webrick/httpservlet/prochandler.rb:26:in `call'

      

+3


source to share


4 answers


As Daniel said, more often than not, the error message will help you quickly find where the error is.

There are indeed some cases where this is not the case.

An easier and faster way to do this binary search is to just insert the wrong string like

<%= the_error_is_after_this_line %>

      

and then move the line until you find the exact line.



I'm not one of those flamboyant programmers who can write tons of lines in time that just works; I usually work out small steps and reload the page in the browser every time.

However, the best way to avoid hard to debug views (or methods or whatever) is to write simple, short ones. My rule of thumb is that I should be able to read the entire view (or method) in the editor window, unless it's just html.

Always use helpers and partial views. Can you count more than two () or [] in your erbie line? If so, use a helper.

Can you count more than two or three blocks in your mind? Use some partial.

+3


source


Not sure if it applies to this problem, but maybe it will help someone. I use rails 5 and if you put

    <% debugger %>

      



in your html.erb file, it pauses the terminal window where your rails server is running. From there you can debug any parameters or variables that are in the html.erb file.

+3


source


In general, Erb errors tell you where they happened. For example, here your error is on line 6 of the erb file. You left out the error message that came up with the backtrace, but that usually tells you what error to look for. For example, in my simple test:

NameError: undefined local variable or method `asdf' for main:Object
  from (erb):7
  from (irb):6

      

It's pretty clear what is going wrong and where.

Can you post more information about your error and the erb that caused it?

0


source


On Rails 5, you can find the default 'byebug' gem in the Gemfile:

    group :development, :test do
      # Call 'byebug' anywhere in the code to stop execution and get a debugger console
      gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
    end

      

Then you can use byebug on your controller, put it wherever you want and many times when you need it, it works as a "breakpoint" and finally start your $ rails server

class UsersController < ApplicationController
   byebug
end

      

On the command line, write help for options, usually use the letter 'c' to go to the next breakpoint, or the letter 'n' to step by step, and ctrl + d to exit.

 (byebug) help

  break      -- Sets breakpoints in the source code
  catch      -- Handles exception catchpoints
  condition  -- Sets conditions on breakpoints
  continue   -- Runs until program ends, hits a breakpoint or reaches a line
  debug      -- Spawns a subdebugger
  delete     -- Deletes breakpoints
  disable    -- Disables breakpoints or displays
  display    -- Evaluates expressions every time the debugger stops
  down       -- Moves to a lower frame in the stack trace
  edit       -- Edits source files
  enable     -- Enables breakpoints or displays
  finish     -- Runs the program until frame returns
  frame      -- Moves to a frame in the call stack
  help       -- Helps you using byebug
  history    -- Shows byebug history of commands
  info       -- Shows several informations about the program being debugged
  interrupt  -- Interrupts the program
  irb        -- Starts an IRB session
  kill       -- Sends a signal to the current process
  list       -- Lists lines of source code
  method     -- Shows methods of an object, class or module
  next       -- Runs one or more lines of code
  pry        -- Starts a Pry session
  quit       -- Exits byebug
  restart    -- Restarts the debugged program
  save       -- Saves current byebug session to a file
  set        -- Modifies byebug settings
  show       -- Shows byebug settings
  skip       -- Runs until the next breakpoint as long as it is different from the current one
  source     -- Restores a previously saved byebug session
  step       -- Steps into blocks or methods one or more times
  thread     -- Commands to manipulate threads
  tracevar   -- Enables tracing of a global variable
  undisplay  -- Stops displaying all or some expressions when program stops
  untracevar -- Stops tracing a global variable
  up         -- Moves to a higher frame in the stack trace
  var        -- Shows variables and its values
  where      -- Displays the backtrace

(byebug)

      

Alternate debug display option (params): In app / views / layouts / application.html.erb, under the render footer and above, specify the following:

<%= debug(params) if Rails.env.development? %>

      

Finally, I share these options as I know as a Ruby on Rails newbie. Hope this helps.

Source for some help: https://rubyplus.com/articles/3631-Debugging-using-ByeBug-Gem-in-Rails-5

0


source







All Articles