问题描述
我正在开发一个新的Rails 3应用程序。控制器中定义的所有实例变量将在我的视图中自动调试。这在开发和生产模式中都会发生。
尽管这很有帮助,我真的希望摆脱这一点,因为它部分地破坏了HTML布局。 / p>
例如我在我的控制器索引操作中:
respond_with @articles = Article.published.order(created_at DESC)。page(params [:page])。per(5))
在视图中,您将自动看到类似于<%= debug @articles%>输出的内容,尽管我从未在视图中调用检查或调试。
一个示例图片来演示整个问题:
我的Gemfile如下所示:
你应该创建一个方法你的应用程序助手模块:
def debug_all& block
excluded_vars = [@lookup_context,@view_context_class ,@action_has_layout]
controller.instance_variables.each do | var |
除非var.at(1)==_或excluded_vars.include?(var)
yield var
end
end
end
在您的应用程序布局中:
<%debug_all do | var | %>
<%=变量名:#{var}%>
<%= eval(var).inspect%>
<%end%>
I'm currently developing a new Rails 3 app. All my instance variables defined in the controllers will automatically be debugged in my views. This happens both in development and production mode.
As much as this is helpful, I'd really like to get rid of that because it destroys the HTML layout partially.
For example I have in my controllers index actions:
respond_with(@articles = Article.published.order("created_at DESC").page(params[:page]).per(5))
In the view you will automatically see something similiar like the output of <%= debug @articles %>, although I never call inspect or debug in my views.
A sample picture to demonstrate the whole issue:
http://www.diigo.com/item/image/16wox/padm?size=o
My Gemfile looks like this: https://gist.github.com/1080104
You should create a method in your application helper module:
def debug_all &block
excluded_vars = ["@lookup_context", "@view_context_class", "@action_has_layout"]
controller.instance_variables.each do |var|
unless var.at(1)== "_" or excluded_vars.include?(var)
yield var
end
end
end
And in your application layout:
<% debug_all do |var| %>
<%= "variable name: #{var} " %>
<%= eval(var).inspect %>
<% end %>
这篇关于在所有视图中调试输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!