binding.pry in Rails View: NameError: undefined local variable or method

binding.pry in Rails View: NameError: undefined local variable or method

我使用 Rails 4 和宝石 byebug, pry-rails, pry-byebug, pry-stack_explorer.

当我在视图文件中替换时:

  <td class="subtotal"><%= order.display_item_total %></td>

  <td class="subtotal"><%= binding.pry %></td>

并在 rails 服务器进程为 运行 的控制台中键入并在 pry 断点处停止执行:

order.inspect

我收到错误消息:

NameError: undefined local variable or method `order' for #<ActionView::OutputBuffer:0x007fdf13d99bb8>

当我将 binding.pry 替换为 order.inspect 时,我在浏览器中获取了订单的对象信息。

我希望我应该能够在控制台的 pry 会话中获取对象顺序。我做错了什么?

编辑: 在控制台输出的开头:

From: /Users/standardnerd/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activesupport-4.2.7.1/lib/active_support/core_ext/string/output_safety.rb @ line 166 ActiveSu
pport::SafeBuffer#safe_concat:

    165: def safe_concat(value)
 => 166:   raise SafeConcatError unless html_safe?
    167:   original_concat(value)
    168: end

这会影响变量的范围吗?

<td class="subtotal"><%= binding.pry %></td>

正在尝试将 binding.pry 的响应输出到视图,我认为这会触发您的 SafeConcatError,我怀疑您没有访问正确的绑定。

最好是做...

<% binding.pry %>
<td class="subtotal"><%= order.display_item_total %></td>

...因此在尝试呈现总数之前立即执行 pry