Rails 5.2.3 content_for 未按规定工作

Rails 5.2.3 content_for not working as specified

所以,我已经阅读了很多关于 content_for 的其他问题报告,但我仍然无法弄清楚我的代码。

我正在尝试从各种视图中添加一些 bootstrap 面包屑数据,根据代码中的位置,每个视图都有不同的 content_for。然后在我的 application.html.erb 中,我调用了一个呈现面包屑数据的部分。但是当我查看源代码时,数据是空的。

我以为我可能有订购问题,我不认为是这种情况,但我不太确定。

这是事件的顺序。我正在调用 new.html.erb 来呈现新视图并设置 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 数据之外的所有内容都显示在源中(和屏幕上。)

但是,我在代码中的 yield:breadcrumb 处一无所获。来源只是显示

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

任何人都可以提供有关此问题的更多见解吗?如果我需要提供更多信息,也请告诉我。我是一名新手 rails 程序员,如有任何帮助,我们将不胜感激。

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

我感谢所有关注该问题的人,对于浪费在本应是一个明显问题上的时间,我深表歉意。至少现在回过头来看。这是一个很好的教学时刻,当你专注于你试图添加的新事物时,不要忽视你认为你很了解的所有细节。经验教训!