viewrenderingyieldruby-on-rails-5.2content-for

Rails 5.2.3 content_for not working as specified


So, I've read through a bunch of other problem reports surrounding content_for and I have still not been able to figure out what is going on in my code.

I'm trying to add some bootstrap breadcrumb data from various views, that each have a different content_for based on the location in the code. Then in my application.html.erb I'm calling a partial that is rendering the breadcrumb data. But the data is empty when I view the source.

I thought I may have a ordering issue, I don't think that is the case but I'm not really sure.

Here is the order of events. I'm calling new.html.erb to render the view for new and also set the content_for data. Then in the layout I'm calling a partial _breadcrumb.html.erb in the body of the main layout to display the content_for data.

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>

I know the partial is being rendered because everything but the content_for data shows up in the source (and on the screen.)

However, I'm getting nothing where the yield :breadcrumb is in the code. The source just shows

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

Can anyone offer any more insight on this issue? If I need to provide more information just let me know that as well. I'm a novice rails programmer and any help would be appreciated.


Solution

  • It turns out this was a routing issue and not a problem with content_for and yield. I had made some directory changes to better organize the app and the route being used was pointing to an older version of the file in a different directory. One which didn't have the content_for setup properly. The route was changed properly in the routes.rb file but not updated correctly in the view reference.

    I appreciate everyone who looked at the problem and I apologize for any time wasted on what should have been an obvious problem. At least looking back at it now. It's a good teaching moment to not overlook all of the details you think you know well when focusing on something new you are trying to add. Lesson learned!