跳至內容 跳至搜尋

Action View Debug Helpers

提供用於簡化 Rails 物件偵錯的一組函式。

函式
D
已包含模組

Instance Public methods

debug(object)

傳回 object 的 YAML 呈現,並以 <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