ruby-on-railsprypry-rails

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


I use Rails 4 with the gems byebug, pry-rails, pry-byebug, pry-stack_explorer.

When i replace in a view file:

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

with

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

and type inside the console where the rails server process is running and the execution stopped at the pry breakpoint:

order.inspect

i get the error message:

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

When i replace binding.pry with order.inspect i get the object info of order in the browser.

I would expect that I should be able to get the object order inside the pry session in the console. What am I doing wrong?

EDIT: In the beginning of the console output:

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

Does this impact the scope of the variables?


Solution

  • The line

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

    Is attempting to output the response from binding.pry to the view, and I think this is triggering your SafeConcatError and I suspect you're not accessing the correct binding.

    Better would be to do...

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

    ...so doing the pry immediately before you attempt to render the total.