Jordans
Jordans

Reputation: 97

Rails is ignoring my application.html.erb file

I am getting some very weird behaviour from my rails app. I have a simple project with nothing generated just the default files. I want to be able to add a nav bar to my application.html.erb file however when I try to render it, nothing appears.

After some playing around I noticed no matter what I type in the application.html.erb file nothing appears, what is even stranger is that if I add funky syntax in which I would expect rails to error out, rails just seems to ignore my application.html.erb file.

Interestingly enough, if I had a render html in my Application Controller my message "Hello" pops up. I am very lost as to what the problem could be, any help would be greatly appreciated!

Note: I found this answer on stackoverflow that pretty much had the same issue however the proposed fix did not work : application.html.erb is still not rendering

Note: I am following along this tutorial to install bootstrap : https://www.youtube.com/watch?v=ryelNMlp-iY

application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title>Thehighwayclinic</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' %>
  </head>

  <body>
    <p> hello world </p>
    <%= render 'navbar' %>
    <%= yield %>
  </body>
</html>

_navbar.html.erb

<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
      <a class="navbar-brand" href="#">Fixed navbar</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarCollapse">
        <ul class="navbar-nav mr-auto">
          <li class="nav-item active">
            <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Link</a>
          </li>
          <li class="nav-item">
            <a class="nav-link disabled" href="#">Disabled</a>
          </li>
        </ul>
        <form class="form-inline mt-2 mt-md-0">
          <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
          <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
        </form>
      </div>
</nav>

application_controller.rb

class ApplicationController < ActionController::Base
    # def hello
    #     render html: "hello, world!"
    # end

    def index
        render html: "hello"
    end
end

routes.rb

Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  # Default rooting on page load
  root 'application#index'
end

Upvotes: 1

Views: 866

Answers (1)

spickermann
spickermann

Reputation: 106872

When using render with the html: option then Rails doesn't use a layout file per default. But you can use the layout: true options to tell Rails to use it anyway:

render html: 'Hello', layout: true

Read about Rendering in the Rails Guides.

Upvotes: 1

Related Questions