调试Ruby erb文件有哪些提示?

时间:2009-08-29 05:31:56

标签: ruby debugging erb rhtml

目前,当我在erb模板上遇到错误(用于HTTPServer / cgi)时,我会执行以下操作:

  • 如果这是一个小改动,则还原,保存并重新测试。
  • 对于大型更改或新文件,删除或注释代码的1/2,然后重新测试。执行二进制搜索,直到我删除/找到损坏的代码。

调用堆栈似乎与我的.rhtml文件中的任何内容都不对应。

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

4 个答案:

答案 0 :(得分:3)

正如Daniel所说,大多数情况下,错误消息将帮助您快速找到错误的位置。

确实有些情况下没有。

这种二元搜索更快的方法就是插入错误的行,比如

<%= the_error_is_after_this_line %>

然后移动直到你找到确切的行。

我不是那些能够每次写出大量线条的聪明程序员之一;我通常通过小步骤开发,每次都在浏览器上重新加载页面。

也就是说,避免难以调试视图(或方法或其他)的更好方法是编写简单的简短视图。我的经验法则是我必须能够在编辑器窗口中读取整个视图(或方法),除非它只是简单的html。

始终使用帮助程序和部分视图。你能在erb视图的一行中计算两个以上()或[]吗?如果是,请使用助手。

您可以在视图中计算超过两三个街区吗?使用一些部分。

答案 1 :(得分:3)

不确定这是否适用于此问题但可能会对某人有所帮助。我正在使用rails 5,如果你把

    <% debugger %>
在你的html.erb文件中,它将暂停运行rails服务器的终端窗口。从那里你可以调试html.erb文件所具有的任何参数或变量。

答案 2 :(得分:0)

一般来说,Erb错误会告诉您它们发生的位置。例如,您的错误位于erb文件的第6行。您已经省略了回溯附带的错误消息,但这通常会告诉您要查找的错误类型。例如,在我的简单测试中:

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

很明显出了什么问题以及在哪里。

您是否可以发布有关错误和导致错误的错误的更多信息?

答案 3 :(得分:0)

在Rails 5上,默认情况下,您可以在Gemfile中找到gem'byebug':

    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

然后,您可以在控制器上使用byebug,将其放置在所需的位置,并且可以多次使用,它的功能就像一个“断点”,最后运行服务器$ rails server

class UsersController < ApplicationController
   byebug
end

在命令行中为选项写帮助,通常使用字母'c'继续下一个断点,或者使用字母'n'逐步前进,并按住ctrl + d退出。

 (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's 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)

显示调试(参数)的其他选项: 在渲染页脚上方和下方的app / views / layouts / application.html.erb文件中,放置下一个:

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

最后,作为Ruby on Rails的新手,我会分享此选项。希望这会有所帮助。

一些帮助的来源:https://rubyplus.com/articles/3631-Debugging-using-ByeBug-Gem-in-Rails-5

相关问题