Rails 5.2.3 content_for无法按指定方式工作

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

我正尝试从各种视图中添加一些引导面包屑数据,每个视图根据代码中的位置具有不同的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",'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的新手程序员,我们将不胜感激。

thebestme_1 回答:Rails 5.2.3 content_for无法按指定方式工作

事实证明这是一个路由问题,而不是 content_for yield 的问题。我进行了一些目录更改,以更好地组织应用程序,并且所使用的路由指向其他目录中文件的较旧版本。一种没有正确设置 content_for 的代码。在route.rb文件中正确更改了路由,但在视图参考中未正确更新路由。

我感谢所有关注此问题的人,对于任何本来应该是显而易见的问题的浪费,我深表歉意。至少现在回头看看。这是个很好的教学时机,当您专注于要添加的新内容时,不要忽略您认为自己熟悉的所有细节。经验教训!

本文链接:https://www.f2er.com/3148351.html

大家都在问