我在我的应用上使用部分渲染数百次,但我遇到了一个问题,我不明白为什么。
我正在递归地调用部分。
#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
这是我的错误:
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
...
答案 0 :(得分:1)
如果它与错误源自的部分相同,那么看起来你正在从其他地方(即不是来自more_specific
文件)渲染special_content
部分而不是传递所需的变量通过locals
。因为否则,我不认为这个错误是可能的。
您也可以尝试简单的render
语法,如下所示:
render path_to_parent_partial, shops: @shops, shop: @shop # in index
render path_to_child_partial, shops: shops, shop: shop # in parent
但首先,在项目中搜索'more_specific'
并查看您未传递shops
变量的位置。您还可以检查Rails服务器日志输出以查看正在呈现的视图文件。
答案 1 :(得分:0)
您可以直接在#medias / _more_specific.html.slim中调用@shops,因为它是实例变量,所以它也可以在这里使用。