Rails: how to add a header to every page

Drew Johnson picture Drew Johnson · May 25, 2010 · Viewed 15.3k times · Source

What is the standard way to add a header and footer to every view in a Rails app?

Answer

maček picture maček · May 25, 2010

If this file is found, it will be used.

app/views/layouts/application.html.erb

<!doctype html>
<html>
  <head>
    <!-- stuff -->
  </head>
  <body>
    <!-- this is where content will be rendered -->
    <%= yield %>
  </body>
</html>

Otherwise, you can call in a different one.

# app/controllers/birds_controller.rb
class BirdsController < ApplicationController

  layout :birds   # looks for app/views/layouts/birds.html.erb

  # ...
end