调试 Ruby erb 文件有哪些技巧?

发布于 2024-08-03 05:41:09 字数 478 浏览 5 评论 0原文

目前,当我在 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'

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

  • If it's a small change, revert, save and retest.
  • For a large change or new file, delete or comment 1/2 the code, and retest. Perform a binary search until I've deleted/found the broken code.

The call stack doesn't seem to correspond to 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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

信仰 2024-08-10 05:41:10

不确定这是否适用于这个问题,但也许会对某人有所帮助。我使用的是 Rails 5,如果您放入

    <% debugger %>

html.erb 文件,它将暂停您的 Rails 服务器正在运行的终端窗口。从那里您可以调试 html.erb 文件具有的任何参数或变量。

Not sure if this is applicable to this problem but maybe it will help someone. I'm using rails 5 and if you put

    <% debugger %>

in your html.erb file it will pause the terminal window that your rails server is running in. From there you can debug whatever params or variables your html.erb file has.

九八野马 2024-08-10 05:41:10

正如大牛所说,大多数时候错误信息会帮助你快速找到错误所在。

确实有一些情况并非如此。

进行二分搜索的更笨、更快的方法是插入错误的行,然后

<%= the_error_is_after_this_line %>

移动该行,直到找到确切的行。

我不是那些每次可以编写大量行的聪明程序员之一;我不是那些聪明的程序员之一。我通常通过小步骤进行开发,并每次在浏览器上重新加载页面。

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

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

你能数出你视野中超过两三个街区吗?使用一些部分。

As Daniel said, most times the error message will help you to quickly find where the error is.

There are indeed some occasions on which it doesn't.

The dumber, faster way to do that binary search is to just insert a wrong line, like

<%= the_error_is_after_this_line %>

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

I'm not one of those bright programmers who can write tons of lines per time which just work; i usually develop by small steps and reload the page on browser each time.

That said, the better way to avoid hard to debug views (or methods, or whatever) is to write simple, short ones. My rule of thumb is that I must be able to read the whole view (or method) in the editor window, unless it's just plain html.

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

Can you count more than two or three blocks in your view? Use some partials.

千寻… 2024-08-10 05:41:10

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

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

很清楚出了什么问题以及出在哪里。

您能否发布有关您的错误以及导致该错误的 erb 的更多信息?

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

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

It is clear enough what is going wrong and where.

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

蝶…霜飞 2024-08-10 05:41:10

在 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服务器

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-使用-ByeBug-Gem-in-Rails-5进行调试

On Rails 5 you can find the gem 'byebug' by default at 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 you need, it functions like a 'breakpoint' and finally run your server $ rails server

class UsersController < ApplicationController
   byebug
end

At command line write help for options, normally use letter 'c' to continue to the next breakpoint, or the letter 'n' to advance 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'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)

Other option to display the debug(params):
In the app/views/layouts/application.html.erb file under render footer and above put the next:

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文