如何使用rails控制台进行调试并放入应用程序 [英] How to debug using rails console and puts in application
问题描述
下面是代码 -
def show
puts'in show method'
@post = Feed.find_by_id params [:id]
puts @ post.inspect
redirect_to root_path,除非是@ post.present ?
end
现在我已经通过命令打开服务器 rails server
。另外,在另一个终端我给了命令 rails console
,它打开了irb提示符。当在浏览器中运行 localhost:3000 / posts / 82
它会提供正确的帖子,但没有显示在控制台中。我失踪了几步?调用特定方法时,我想在控制台中打印一些东西。
最好的调试方法是使用 debugger
命令。
如果您使用的是ruby 2.0或更高版本,则必须使用 gem 'byebug'
如果你使用1.9或更低版本,那么 gem ruby-debug
然后,当您在开发模式下运行服务器时,当服务器到达调试器
时,您的服务器将停止,让您可以查看对象的状态并修改它们(比只需使用 puts
该程序将在您的服务器运行的同一窗口中停止。
一些基本命令:
- c 继续执行,直到下一个调试器发现
- n 运行下一个命令如果它是一个函数执行
函数 - s 进入下一个命令,如果是
函数,您将进入该函数并查看变量 - 显示表达式在每一步显示您编写的
表达式的结果(调试时非常有用)循环) - undisplay expression_number 停止显示表达式
- 显示显示所有表达式显示
- list 显示正在执行的源代码
- help 显示可用的命令< kbd> help
comand_name 显示有关命令的详细信息
有关调试的更多信息: http://guides.rubyonrails.org/debugging_rails_applications.html
I want to get some lines printed in irb opened through rails console. I have seen a lot of SO questions of how to achieve it. But I get nothing in the irb.
below is the code--
def show
puts 'in show method'
@post = Feed.find_by_id params[:id]
puts @post.inspect
redirect_to root_path unless @post.present?
end
now I have opened server by command rails server
. Also, In another terminal I gave the command rails console
, it opened the irb prompt. when in browser I run localhost:3000/posts/82
it gives the correct post, but nothing is shown in the console. What step am I missing? I want to print something in the console when a particular method is called.
Best way to debug is to use the debugger
command.
If you are using ruby 2.0 or above, you have to use the gem 'byebug'
and if you are using 1.9 or below, then gem ruby-debug
then, when you run your server in development mode, your server will stop when it reaches the debugger
allowing you to see your objects' state and modify them (much better than simply using puts
The program will stop in the same window that your server runs.
Some basic commands:
- 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 comand_name shows detailed info about a command
More info about debugging: http://guides.rubyonrails.org/debugging_rails_applications.html
这篇关于如何使用rails控制台进行调试并放入应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!