Rails 5.2.3 content_for未按规定工作



因此,我已经阅读了许多其他关于content_for的问题报告,但我仍然无法弄清楚我的代码中发生了什么。

我试图添加一些来自不同视图的引导程序breadcrumb数据,每个视图根据代码中的位置都有不同的content_for。然后在我的application.html.erb中,我调用一个正在呈现面包屑数据的分部。但当我查看源时,数据是空的。

我想我可能有订购问题,我不认为是这样,但我真的不确定。

以下是事件的顺序。我调用new.html.erb为new呈现视图,并设置content_for数据。然后在布局中,我在主布局的主体中调用部分_breadcrumb.html.erb来显示content_for数据。

new.html.erb

<% content_for :breadcrumb do %>
<li class="breadcrumb-item"><%= link_to "Home", root_path %></li>
<li class="breadcrumb-item"><%= link_to "Examples", examples_path %></li>
<li class="breadcrumb-item active">Horizontal Form</li>
<% end %>
<div class="app-example container">
<div class="row">
<%= render partial: "bootstrap", layout: "card_bootstrap", locals: { title: "Horizontal Form" } %>
<%= render partial: "form",      layout: "card_simple_form", locals: { title: "Horizontal Form" } %>
</div>
<%= render partial: "shared/tipps" %>
<%# move modals to bottom %>
<%= render partial: "shared/modal", locals: { id: "modal-bootstrap", title: "Bootstrap",
content: render_source(File.read(File.join(Rails.root, 
'app/views/examples/horizontals/_bootstrap.html.erb'))) } %>
<%= render partial: "shared/modal", locals: { id: "modal-simpleform", title: "Simple Form",
content: render_source(File.read(File.join(Rails.root, 
'app/views/examples/horizontals/_form.html.erb'))) } %>
</div>

_breadcrumb.html.erb

<div class="app-breadcrumb container">
<div class="d-flex flex-row justify-content-between">
<ol class="breadcrumb bg-transparent px-0">
<%= yield :breadcrumb %>
</ol>
<div>
<button type="button" class="btn btn-sm btn-outline-secondary mt-2 mr-2 d-none" data-                tooltip data-placement="bottom" data-trigger="hover" title="compare by clicking fast" data-toggle="button" aria-pressed="false" autocomplete="off">
Swap
</button>
<div class="btn-group btn-group-toggle mt-2 d-none" data-toggle="buttons">
<label class="btn btn-sm btn-outline-secondary">
<input type="radio" name="view" id="stack" autocomplete="off"> Stack
</label>
<label class="btn btn-sm btn-outline-secondary active">
<input type="radio" name="view" id="split" autocomplete="off" checked> Split
</label>
</div>
</div>
</div>
</div>

application.html.erb

<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag "application", media: "all",
"data-turbolinks-track": 'reload' %>
<%= javascript_include_tag "application", "data-turbolinks-track": 'reload' %>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body data-spy="scroll" data-target=".bs-docs-sidebar">
<%= render "header" %>
<div class="container">
<%= render "flash" %>
<%= render "breadcrumb" %>
<%= yield %>
<%= render "footer" %>
<%= debug(params) if Rails.env.development? %>
<!-- This debug code shows the HTTP headers/environment -->
<!-- ul>
<% request.env.each do |item| %>
<li><%= item[0] %> : <%= item[1] %></li>
<% end %>
</ul -->
</div>
</body>
</html>

我知道部分正在渲染,因为除了content_for数据之外的所有内容都显示在源中(和屏幕上(

然而,我在代码中没有得到任何收益:breadcrumb。来源只是显示

<ol class="breadcrumb bg-transparent px-0">
</ol>

有人能就这个问题提供更多见解吗?如果我需要提供更多信息,也请告诉我。我是一个新手rails程序员,任何帮助都将不胜感激。

事实证明,这是一个路由问题,而不是content_foryield的问题。我做了一些目录更改,以更好地组织应用程序,使用的路径指向不同目录中的旧版本文件。一个没有正确设置content_for的。路由在routes.rb文件中已正确更改,但在视图引用中未正确更新。

我感谢所有关注这个问题的人,我为浪费在本应是一个明显问题上的时间道歉。至少现在回过头来看。这是一个很好的教学时刻,当你专注于你试图添加的新内容时,不要忽视你认为自己熟悉的所有细节。吸取教训!

最新更新