简体   繁体   中英

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 
...

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 (ie 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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM