调试 Ruby erb 文件有哪些技巧?
目前,当我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不确定这是否适用于这个问题,但也许会对某人有所帮助。我使用的是 Rails 5,如果您放入
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
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.
正如大牛所说,大多数时候错误信息会帮助你快速找到错误所在。
确实有一些情况并非如此。
进行二分搜索的更笨、更快的方法是插入错误的行,然后
移动该行,直到找到确切的行。
我不是那些每次可以编写大量行的聪明程序员之一;我不是那些聪明的程序员之一。我通常通过小步骤进行开发,并每次在浏览器上重新加载页面。
也就是说,避免难以调试视图(或方法,或其他)的更好方法是编写简单、简短的视图。我的经验法则是,我必须能够在编辑器窗口中读取整个视图(或方法),除非它只是纯 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
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.
一般来说,Erb 错误会告诉您它们发生的位置。例如,您的错误位于 erb 文件的第 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:
It is clear enough what is going wrong and where.
Can you post more information about your error and the erb that caused it?
在 Rails 5 上,你可以在 Gemfile 中默认找到 gem 'byebug':
然后你可以在你的控制器上使用 byebug,把它放在你想要的任何地方,并且多次需要,它的功能就像一个“断点”,最后运行你的服务器 $ Rails服务器
在命令行中编写选项的帮助,通常使用字母“c”继续到下一个断点,或使用字母“n”逐步前进,然后按ctrl+d退出。
显示调试(参数)的其他选项:
在 app/views/layouts/application.html.erb 文件的渲染页脚和上面放置下一个:
最后,我作为 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:
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
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.
Other option to display the debug(params):
In the app/views/layouts/application.html.erb file under render footer and above put the next:
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