title: How to pass multiple locals to a nested partial
body: <p>This should be extremely straightforward and well documented, and I've done it several times, although there's something that's still killing me. </p>
  
  <p>I have a structure of partials calling nested partials.</p>
  
  <p>At some point one <code>render</code> call needs to pass an extra variable to the partial, although the rendering of the partial fails with a: </p>
  
  <pre><code>undefined local variable or method `&lt;variable name&gt;' for #&lt;#&lt;Class:....&gt;
  </code></pre>
  
  <p>Here's my code for calling the <code>render</code>:</p>
  
  <pre><code>= f.simple_fields_for :orders do |c|
    = render partial: "fields", locals: {f: c, step: f.object.step}
  </code></pre>
  
  <p>though this doesn't work either: </p>
  
  <pre><code>= f.simple_fields_for :orders do |c|
    = render "fields", f: c, step: f.object.step
  </code></pre>
  
  <p>here's where the exception is raised: </p>
  
  <pre><code>f.input :quantity, input_html: {step: step}
  </code></pre>
  
  <p>the form_for comes from the <code>views/lists/_form.html.haml</code>:</p>
  
  <pre><code>= simple_form_for( @order, :html =&gt; { :multipart =&gt; true }, defaults: { input_html: { class: 'input-medium' } } ) do |f|
  </code></pre>
  
  <p><code>f</code> is then passed to <code>views/orders/_order_forms.html</code> via</p>
  
  <pre><code>= render "orders/order_forms", f: f
  </code></pre>
  
  <p>here's the exception with trace:</p>
  
  <pre><code>    ActionView::Template::Error (undefined local variable or method `step' for #&lt;#&lt;Class:0x007fe0479ba2b0&gt;:0x007fe04256a930&gt;):
  
  application trace:
      app/views/orders/_fields.html.haml:9:in `_app_views_orders__fields_html_haml___1860431911739668171_70300581339300'
      app/views/orders/_order_forms.html.haml:30:in `_app_views_orders__order_forms_html_haml__2241963939037094859_70300612771460'
      app/views/lists/_form.html.haml:48:in `block in _app_views_lists__form_html_haml__1669043093238943449_70300583658680'
      app/views/lists/_form.html.haml:3:in `_app_views_lists__form_html_haml__1669043093238943449_70300583658680'
      app/views/lists/new.html.erb:3:in `_app_views_lists_new_html_erb___1563391577928218041_70300593681100'
      app/controllers/lists_controller.rb:67:in `new'
  </code></pre>
  
  <p>framework trace (the end of it): </p>
  
  <pre><code>actionpack (3.2.8) lib/action_view/template.rb:145:in `block in render'
  activesupport (3.2.8) lib/active_support/notifications.rb:125:in `instrument'
  actionpack (3.2.8) lib/action_view/template.rb:143:in `render'
  actionpack (3.2.8) lib/action_view/renderer/partial_renderer.rb:265:in `render_partial'
  actionpack (3.2.8) lib/action_view/renderer/partial_renderer.rb:238:in `block in render'
  actionpack (3.2.8) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
  activesupport (3.2.8) lib/active_support/notifications.rb:123:in `block in instrument'
  activesupport (3.2.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (3.2.8) lib/active_support/notifications.rb:123:in `instrument'
  actionpack (3.2.8) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
  actionpack (3.2.8) lib/action_view/renderer/partial_renderer.rb:237:in `render'
  actionpack (3.2.8) lib/action_view/renderer/renderer.rb:41:in `render_partial'
  actionpack (3.2.8) lib/action_view/helpers/rendering_helper.rb:27:in `render'
  haml (4.0.3) lib/haml/helpers/action_view_mods.rb:10:in `block in render_with_haml'
  haml (4.0.3) lib/haml/helpers.rb:89:in `non_haml'
  haml (4.0.3) lib/haml/helpers/action_view_mods.rb:10:in `render_with_haml'
  cocoon (1.2.0) lib/cocoon/view_helpers.rb:40:in `block in render_association'
  </code></pre>
  
  <p>I've had a similar issue before and I resolved it in simplifying the passing of locals.. but now I would really like to understand what's going on. </p>
  
  <p>any clue? </p>
  
  <p>I'm using: </p>
  
  <pre><code>ruby 2.0.0p297
  rails 3.2.8
  </code></pre>
  
  <p>Thanks a lot in advance..</p>
  
  <p><strong>UPDATE</strong> </p>
  
  <p>I have debugged my view and figured that the <code>order_forms</code> is being rendered twice, the first time <code>step</code> is not set, while in the second rendering it is set correctly. </p>
  
  <p>I'm not sure why this happens, but I managed to work it around with adding the following line to my <code>fields.html.haml</code>.</p>
  
  <pre><code>-step = step || 1
  </code></pre>
  
  <p>basically I put a default value to step, in case it's not defined, so that at the first execution the rendering doesn't crash, while at the second execution it works properly. </p>
  
  <p>The page looks as expected now. Although I'm thinking about the waste of resources when rendering the stuff twice. </p>
  
  <p>any idea on why that happens?</p>