【发布时间】:2018-03-13 05:42:08
【问题描述】:
我正在尝试使用某个布局文件渲染部分内容。但能够为该特定部分指定唯一值。以下是我拥有的当前文件,但所发生的只是yield 关键字都显示了集合值。我想传递一个自定义的“标题”并在“正文”中显示集合。
application/_news_article.html.erb
<!-- application/_news_article.html.erb -->
<% if news_article.report_text.present? %>
<div class="news_article_entry">
<div class="text-box">
<span><%= "#{news_article.published_date.strftime("%D")}" %></span> -
<%= "#{news_article.report_text}" %>
</div>
<% if news_article.impact_text.present? %>
<div class="impact-text" data-toggle="tooltip" data-placement="bottom"
title="<%= news_article.impact_text %>">Analysis</div>
<% end %>
</div>
<hr />
<% end %>
application/_news_articles.html.erb
<!-- application/_news_articles.html.erb -->
<%= render partial: "news_article", collection: articles %>
layouts/_panel_box_scrollable.html.erb
<!-- layouts/_panel_box_scrollable.html.erb -->
<div class="panel panel-default skill-evaluation-box">
<div class="skill-heading">
<div class="row">
<div class="col-xs-12">
<%= "#{yield :title}".upcase %>
</div>
</div>
</div>
<div class="skill-body scrollable">
<%= yield :body %>
</div>
</div>
schools/show.html.erb
<!-- schools/show.html.erb -->
<%= render partial: 'news_articles', layout: "layouts/panel_box_scrollable", locals: { articles: @articles } %>
【问题讨论】:
-
您可以使用
content_for(:something) do ... end块显示在您匹配的yield(:something)中 -
@MrYoshiji,我一直在尝试但没有运气 - 我应该在哪个文件中尝试?我一直把它放在
application/_news_articles.html.erb -
您是否尝试将
<% content_for(:title) { "some test ttitle" } %>放入您的任何部分?