如何输出到网站而不是控制台进行Rails调试?

时间:2022-05-18 20:45:55

Well the title explains pretty much everything. I want to use something like

题目解释了一切。我想用一些类似的东西。

p something

And be able to see the output in the website, just for the sake of debugging.

并且能够在网站上看到输出,只是为了调试。

2 个解决方案

#1


6  

I suspect you're coming from PHP, because this is a very PHP-esque thing to want to do. Standard output isn't sent to the browser in Rails, something PHP developers take for granted.

我猜你来自PHP,因为这是PHP风格的东西。标准输出不是在Rails中发送给浏览器的,PHP开发人员认为这是理所当然的。

If you want to see something in browser, you need to render it.

如果你想在浏览器中看到什么,你需要渲染它。

From a view you can use the debug helper to "vardump" a value:

从视图中,您可以使用调试助手来“vardump”一个值:

<%= debug something %>

From a controller you can quickly see the value of a variable using render :inline => p(something) or render :inline => something.inspect. Make sure you return afterwards or otherwise prevent yourself from reaching a second render call.

从控制器中,您可以使用render:inline => p(something)或render:inline => something.inspect快速查看变量的值。确保您在事后返回或防止您自己到达第二个渲染调用。

You should get used tailing your log files, and making use of the Rails logger.

您应该习惯跟踪日志文件,并使用Rails日志记录器。

#2


3  

I would suggest piggy-backing off of the Rails flash maps. In a controller, use the following:

我建议在Rails的flash地图上使用piggy。在控制器中,使用以下内容:

 flash[:log] = "<li>Log: #{log_info}</li>"

Then in your view (possibly in your layout) use:

然后在你的视图中(可能在你的布局中)使用:

 <% if !flash[:log].blank? && RAILS_ENV != 'production' %>
   <ul class="logs">
     <%= flash[:log] %>
   </ul>
 <% end %>

That should put it out on your page!

那应该把它放在你的页面上!

You can also put the following in ApplicationController

您还可以在ApplicationController中放置以下内容

 def log(msg)
   flash[:log] ||= ""
   flash[:log] += "<li>#{Time.new.to_s} - #{msg}</li>"
 end

Then simply call from any controller:

然后简单地从任何控制器调用:

 log("Hi *.")

Or from any view:

或从任何视图:

 @controller.log("Hi there, you!")

#1


6  

I suspect you're coming from PHP, because this is a very PHP-esque thing to want to do. Standard output isn't sent to the browser in Rails, something PHP developers take for granted.

我猜你来自PHP,因为这是PHP风格的东西。标准输出不是在Rails中发送给浏览器的,PHP开发人员认为这是理所当然的。

If you want to see something in browser, you need to render it.

如果你想在浏览器中看到什么,你需要渲染它。

From a view you can use the debug helper to "vardump" a value:

从视图中,您可以使用调试助手来“vardump”一个值:

<%= debug something %>

From a controller you can quickly see the value of a variable using render :inline => p(something) or render :inline => something.inspect. Make sure you return afterwards or otherwise prevent yourself from reaching a second render call.

从控制器中,您可以使用render:inline => p(something)或render:inline => something.inspect快速查看变量的值。确保您在事后返回或防止您自己到达第二个渲染调用。

You should get used tailing your log files, and making use of the Rails logger.

您应该习惯跟踪日志文件,并使用Rails日志记录器。

#2


3  

I would suggest piggy-backing off of the Rails flash maps. In a controller, use the following:

我建议在Rails的flash地图上使用piggy。在控制器中,使用以下内容:

 flash[:log] = "<li>Log: #{log_info}</li>"

Then in your view (possibly in your layout) use:

然后在你的视图中(可能在你的布局中)使用:

 <% if !flash[:log].blank? && RAILS_ENV != 'production' %>
   <ul class="logs">
     <%= flash[:log] %>
   </ul>
 <% end %>

That should put it out on your page!

那应该把它放在你的页面上!

You can also put the following in ApplicationController

您还可以在ApplicationController中放置以下内容

 def log(msg)
   flash[:log] ||= ""
   flash[:log] += "<li>#{Time.new.to_s} - #{msg}</li>"
 end

Then simply call from any controller:

然后简单地从任何控制器调用:

 log("Hi *.")

Or from any view:

或从任何视图:

 @controller.log("Hi there, you!")