brcebn
brcebn

Reputation: 1722

Render partial with locals

I'm using render with partials hundred times on my app but I've a problem with one and I don't understand why.

I'm calling partial recursively.

#index.html.slim
= render partial: 'medias/special_content', locals: {shops: @shops, shop: @shop}

#medias/_special_content.html.slim
= render partial: 'medias/more_specific', locals: {shops: shops, shop: shop}

#medias/_more_specific.html.slim
- if shops.nil?
  #Some code

Here is my error:

undefined local variable or method `shops' for #<#

<Class:0x0000000775c360>:0x007f027cff5c28>
    app/views/medias/_more_specific.html.slim:130:in
    `_app_views_medias__more_specific_html_slim___1518734296928926472_69824330512820' 
actionpack (3.2.17) lib/action_view/template.rb:145:in `block in render' 
activesupport (3.2.17) lib/active_support/notifications.rb:125:in `instrument' 
actionpack (3.2.17) lib/action_view/template.rb:143:in `render' 
actionpack (3.2.17) lib/action_view/renderer/partial_renderer.rb:265:in `render_partial' 
actionpack (3.2.17) lib/action_view/renderer/partial_renderer.rb:238:in `block in render' 
actionpack (3.2.17) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument' 
activesupport (3.2.17) lib/active_support/notifications.rb:123:in `block in instrument' 
activesupport (3.2.17) lib/active_support/notifications/instrumenter.rb:20:in `instrument' 
activesupport (3.2.17) lib/active_support/notifications.rb:123:in `instrument' 
actionpack (3.2.17) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument' 
actionpack (3.2.17) lib/action_view/renderer/partial_renderer.rb:237:in `render' 
actionpack (3.2.17) lib/action_view/renderer/renderer.rb:41:in `render_partial' 
actionpack (3.2.17) lib/action_view/renderer/renderer.rb:15:in `render' 
actionpack (3.2.17) lib/action_view/helpers/rendering_helper.rb:24:in `render' 
app/views/medias/_special_content.html.slim:20:in 
...

Upvotes: 0

Views: 2518

Answers (2)

SHS
SHS

Reputation: 7744

If it's the same partial where the error is originating from, then it looks like you're rendering the more_specific partial from somewhere else (i.e. not from the special_content file) and not passing the required variables through locals. Because otherwise, I don't think this error is possible.

You could also try the simple render syntax, like so:

render path_to_parent_partial, shops: @shops, shop: @shop # in index
render path_to_child_partial, shops: shops, shop: shop # in parent

But first, search for 'more_specific' in your project and see where you aren't passing the shops variable. You can also check the Rails Server logs output to see which view files are being rendered.

Upvotes: 1

ROR
ROR

Reputation: 441

You can direct call @shops in #medias/_more_specific.html.slim because it is instance variable so it would also available here.

  • if @shops.blank?

Upvotes: 0

Related Questions