ZAR
ZAR

Reputation: 2736

application.html.erb is still not rendering

I generated a Rails application, and am playing around with the internals. Previously my application.html.erb was rendering properly, but now it seems like Rails is totally ignoring it because it won't even generate an error.

There have been a bunch of questions on Stack Overflow regarding this problem. I've looked at what I think is all of them, but none have helped.

My routes:

Rails.application.routes.draw do

  # static_pages from rails tutorial ch. 3
  get 'static_pages/home'
  get 'static_pages/help'
  get 'static_pages/about'

end

Here is the views/layout/application.html.erb

<!DOCTYPE html>
<html>
    <head>
        <title>This Title is not showing up</title>
        <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
        <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
        <%= csrf_meta_tags %>
    </head>
    <body>
    <p> why isnt this showing up?? </p>
        <%= yield %>

    </body>
</html>

Here is the static_pages_controller:

class StaticPagesController < ApplicationController   
    layout 'application' #<- I know this shouldn't be necessary, but I thought i'd try

    def initialize
        @locals = {:page_title => 'Default'}
    end

    def about
        @locals[:page_title] = 'About'
        render @locals
    end

    def help
        @locals[:page_title] = 'Help'
        render @locals
    end

    def home
        @locals[:page_title] = 'Home'
        render @locals
    end

end

Here is the Application Controller:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
end

There are no other layouts. My Views folder has the following structure:

-Views
 |-layouts
 ||-application.html.erb
 |
 |-static_pages
 ||-about.html.erb
 ||-home.html.erb
 ||-help.html.erb

I've tried purposefully generating an error in the application.html.erb, calling variables that don't exist and whatever other shenanigans. Rails is totally ignoring me and I'm feeling insecure.

All I wanted to do is to display the page name in the <title>, but I can't even get plaintext to render correctly. How can I get this to work so that I can properly fail at getting the controller variable in the title?

Upvotes: 2

Views: 1324

Answers (1)

Dmitry Sokurenko
Dmitry Sokurenko

Reputation: 6132

You should not override the controller initialize method. Doing this will break the base class behavior.

While, I believe, just calling the super from the initialize will fix your issue, the correct Rails way to initialize a controller for a specific action is to use a before filter instead.

Example:

class StaticPagesController < ApplicationController   
  layout 'application'

  before_action :load_locals

  def load_locals
    @locals = {:page_title => 'Default'}
  end

  ...
end

Upvotes: 4

Related Questions