【问题标题】:Rails 5.2.3 content_for not working as specifiedRails 5.2.3 content_for 未按规定工作
【发布时间】:2020-03-03 12:20:51
【问题描述】:

所以,我已经阅读了一堆围绕 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",
      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 程序员,如有任何帮助,将不胜感激。

【问题讨论】:

    标签: view rendering yield ruby-on-rails-5.2 content-for


    【解决方案1】:

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

    感谢所有关注此问题的人,对于在本应是显而易见的问题上浪费的任何时间,我深表歉意。至少现在回头看。这是一个很好的教学时刻,在专注于尝试添加的新事物时,不要忽视所有你认为自己熟悉的细节。吸取教训!

    【讨论】:

      猜你喜欢
      • 2014-01-30
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 2018-08-12
      • 2022-08-22
      • 1970-01-01
      • 1970-01-01
      • 2013-10-09
      相关资源
      最近更新 更多