我想在通过 Rails 控制台打开的 irb 中打印一些行。我见过很多关于如何实现它的问题。但我在 irb 中什么也没得到。
下面是代码——
def show
puts 'in show method'
@post = Feed.find_by_id params[:id]
puts @post.inspect
redirect_to root_path unless @post.present?
end
现在我已经通过命令打开服务器rails server
。另外,在另一个终端我给出了命令rails console
,它打开了 irb 提示符。当我在浏览器中运行时localhost:3000/posts/82
它给出了正确的帖子,但控制台中没有显示任何内容。我缺少哪一步?我想在调用特定方法时在控制台中打印一些内容。
最好的调试方法是使用debugger
命令。
如果您使用 ruby 2.0 或更高版本,则必须使用gem 'byebug'
如果您使用的是 1.9 或更低版本,那么gem ruby-debug
然后,当您在开发模式下运行服务器时,您的服务器将在达到debugger
允许您查看对象的状态并修改它们(比简单地使用要好得多puts
该程序将在服务器运行的同一窗口中停止。
一些基本命令:
-
c continues the execution until next debugger is found
-
n runs the next command. If it is a function executes the
function
-
s step into the next command. If it is a
function, you will get into the function and see the variables
-
display expression on every step display the result of the
expression you write (very useful when debugging loops)
-
undisplay expression_number stops displaying the expresion
-
display shows all the expressions being displayed
-
list Displays the source code being executed
-
help shows the available commands help
command_name shows detailed info about a command
有关调试的更多信息:http://guides.rubyonrails.org/debugging_rails_applications.html http://guides.rubyonrails.org/debugging_rails_applications.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)