Partial Layout in Rails 3.0.0
I have a collection of partials being rendered using a layout for each element (wrapping in a container). Howe开发者_StackOverflow中文版ver, when rendering the collection, an outer 'container' is also added (it appears to be adde to each render, despite no layout being specified.
Example:
# index.html.erb
<%= render :partial => 'sprockets' %>
# _sprockets.html.erb
<%= render :partial => 'sprocket', :layout => 'container' %>
<%= render :partial => 'sprocket', :layout => 'container' %>
<%= render :partial => 'sprocket', :layout => 'container' %>
# _sprocket.html.erb
...
# _container.html.erb
<div class="container"><%= yield %></div>
Gives:
<div class="sprocket">
<div class="sprocket">
...
</div>
<div class="sprocket">
...
</div>
<div class="sprocket">
...
</div>
</div>
I seem to remember being able to do this in Rails 2.3.8. Note the above is a simplification of my code (I'd like to keep the layouts and multi-partial format). Any ideas what I'm doing wrong? Thank!
You are probably exploiting a quirk in ActionView. To the best of my knowledge, layouts are not meant to be used with partials like that. My guess is, every time you do :layout => 'container'
, it sets the same instance variable. Since ActionView renders partials inside-out (innermost partial gets rendered first), the last used value for :layout
also gets used higher up the chain.
Maybe doing something like
# index.html.erb
<%= render :partial => 'sprockets', :layout => false %>
will help.
Still I'd say this is not a proper way to get the results you want.
精彩评论