跳到内容 跳到搜索

Action View 调试助手

提供一组方法,以便更轻松地调试 Rails 对象。

方法
D
包含的模块

实例公共方法

debug(object)

返回一个 YAML 表示形式,该表示形式由 object 用 <pre> 和 </pre> 包裹。如果无法使用 to_yaml 将对象转换为 YAML,则会调用 inspect。这对于在渲染时检查对象很有用。

@user = User.new({ username: 'testing', password: 'xyz', age: 42})
debug(@user)
# =>
<pre class='debug_dump'>--- !ruby/object:User
attributes:
  updated_at:
  username: testing
  age: 42
  password: xyz
  created_at:
</pre>
# File actionview/lib/action_view/helpers/debug_helper.rb, line 28
def debug(object)
  Marshal.dump(object)
  object = ERB::Util.html_escape(object.to_yaml)
  content_tag(:pre, object, class: "debug_dump")
rescue # errors from Marshal or YAML
  # Object couldn't be dumped, perhaps because of singleton methods -- this is the fallback
  content_tag(:code, object.inspect, class: "debug_dump")
end