在 Ruby on Rails 上,我们如何在控制器内打印调试信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3347847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
On Ruby on Rails, how do we print debug info inside of a controller?
提问by nonopolarity
In PHP, CGI, or in RoR's View, we can easily print out debug information. How about in the Controller, how can we just say, print "hello world"(to the webpage output) and return to continue with the view, or stop the controller right there?
在 PHP、CGI 或 RoR 的 View 中,我们可以轻松打印出调试信息。在控制器中,我们怎么能说,print "hello world"(到网页输出)并返回继续查看,或者在那里停止控制器?
回答by Papipo
In the controller you can:
在控制器中,您可以:
render :text => @some_object.inspect
But your view won't be rendered.
但是您的视图不会被渲染。
You could also:
你也可以:
Rails.logger.debug("My object: #{@some_object.inspect}")
and run tail on log/development.log to see the output.
并在 log/development.log 上运行 tail 以查看输出。
In the view the recommeneded way is:
在视图中推荐的方式是:
<%= debug(@some_object) %>
回答by Nikita Rybak
Don't know about print, but putsnever failed me. Your hello worldwill be in console and logs and normal flow will continue.
Did I understand you correctly?
不知道print,但puts从来没有让我失望。您hello world将在控制台和日志中,正常流程将继续。
我对你的理解正确吗?
回答by Flavius Stef
You can debug in the view: <%= debug @post %>. More info: http://guides.rubyonrails.org/debugging_rails_applications.html
您可以在视图中调试:<%= debug @post %>。更多信息:http: //guides.rubyonrails.org/debugging_rails_applications.html
回答by mpalencia
you can use abort with inspect
您可以将中止与检查一起使用
abort params[:whatevs].inspect
or
或者
abort @my_variable.inspect
for JSONformat
对于JSON格式
render json: JSON.pretty_generate(JSON.parse(@my_variable.to_json))
回答by nonopolarity
Actually, Nikita's suggestion isn't bad. But usually it is hard to look at the data in the terminal because the screen buffer size can be limited and it is full of previous data from previous rendering of pages.
其实,尼基塔的建议也不错。但是通常很难在终端中查看数据,因为屏幕缓冲区大小可能会受到限制,并且充满了之前页面渲染的数据。
But, say on the Mac, just make sure the screen buffer is large enough, and also use in the rails serverterminal the key Cmdkto clear the whole screen buffer first, and use the browser to fetch a webpage, and now use Cmdfto search for the string you printed out, and it works quite well also. Actually if Cmdkis used to first clear the whole screen buffer, then the print out should always fit in the screen buffer.
但是,在Mac上,只要确保屏幕缓冲区足够大,并且在rails server终端中使用键Cmdk先清除整个屏幕缓冲区,然后使用浏览器获取网页,现在用于Cmdf搜索字符串你打印出来了,效果也很好。实际上,如果Cmdk用于先清除整个屏幕缓冲区,那么打印出来的内容应该始终适合屏幕缓冲区。
Update: With Rails 3.x, we can make use of flash.error, flash.notice, flash.now.noticeto print out something in the view if we want to: https://www.rubyguides.com/2019/11/rails-flash-messages/
更新:使用Rails 3.x中,我们可以利用的flash.error,flash.notice,flash.now.notice打印出的东西的看法,如果我们想:https://www.rubyguides.com/2019/11/rails-flash-messages/
Example:
例子:
flash.notice = "DEBUG INFO: some_obj is #{some_obj.inspect}"

