在 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