Partial, Layout, Template rendering problems

牧云@^-^@ 提交于 2020-01-13 16:20:50

问题


The Situation

So when I visit a page, I want to be able to apply a layout to a partial (I have three partials that I want with the same layout). Right now, I am trying to do it with this command:

<%= render :partial => "shared/services/essay", :layout => "layouts/services/tab_pane", :locals => { :service => "essay" } %>

where shared/services/essay goes something like:

<% content_for :intro do %>
<p>
    blah.
</p>
<% end %>

<% content_for :workflow do %>
<div>
    blah.
</div>
<% end %>

<% content_for :value_prop do %>
<p>
    blah.
</p>
<% end %>

and `layouts/services/tab_pane' goes like:

<div class="tab-pane fade in" id=<%= service %> >
    <%= yield :intro %>

    <div class="span7 workflow">
        <h3>Workflow</h3>
        <%= yield :workflow %>
    </div>

    <div class="span5 value-proposition">
        <h3>Our Value Proposition for You</h3>
        <%= yield :value_prop %>
        <div class="call-to-action">
            <%= link_to "Action!", contact_path, class: "btn btn-large btn-warning" %>
        </div>
    </div>
</div>

The Problem

When I load it up, I just get the layout HTML. None of the content is put through. So essentially, the yields aren't pulling in content or something.

Could someone help? Thanks!

Things I've tried

If I try nested layouts as mentioned on Ruby Guides (adding...

<%= render :template => "layouts/services/tab_pane", :locals  => { :service => "essay" } %>

to my partials and making all the proper name changes), my second partial contains content from the first, and my third from both the first and second. So my second partial would contain 2 blah's for each section and my third partial would contain 3 blah's for each section. Thanks!

UPDATE:

The problem is now solved (thanks Michael), but now I guess I was wondering what best practices were when dealing with something like this. Should I avoid something like this in the future and only worry about giving templates/layout to whole pages as opposed to partials within the page? Thanks!


回答1:


I wonder if you are getting hit with the ViewFlow handling of partials. Are you calling this partial several times? The problem is rails stores the content_for data in a hash and its non-destructive.

You could try adding:

def yield_content!(content_key) view_flow.content.delete(content_key) end

in your ApplicationHelper and call yield_content instead of yield in your layout file.



来源:https://stackoverflow.com/questions/13106580/partial-layout-template-rendering-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!