Rails 4-部分上的多个产量块

Enr*_*ent 3 ruby-on-rails ruby-on-rails-4

是否有可能使用多个yield块进行局部处理?我想用它在我的项目上实现引导模式框,有点像这样:

<div class="modal fade" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <%= yield :header %>
            </div>

            <div class="modal-body">
                <%= yield :body %>
            </div>

            <div class="modal-footer">
                <%= yield :footer %>
            </div>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

这或多或少是我在考虑使用它的方式

<%= render partial: "shared/modal" do %>
    <% content_for :header do %>
        ...        
    <% end %> %>

    <% content_for :body do %>
        ...        
    <% end %> %>

    <% content_for :footer do %>
        ...        
    <% end %> %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?由于某种原因,这可能是一个不好的方法吗?

sam*_*ker 6

经过多次试验和错误,我认为我在Rails 5中解决了这个问题,但需要yield在我的局部代码中插入一个空白才能使其正常工作:

_partial.html.erb

<div class="partial">
  <%= yield %> <!--Does not do anything-->
  <div class="header">
    <%= yield :header %>
  </div>
  <div class="text">
    <%= yield :text %>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

实施为:

full_layout.html.erb

<%= render "partial" do %>
  <% content_for :header do %>
    <h1>Header content</h1>
  <% end %>
  <% content_for :text do %>
    <p>Text content</p>
  <% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

  • 恩里克(Enrique),如果该解决方案对您有用,您介意将其标记为接受吗? (2认同)