Skip to content

rails default application layout

Daniel Kehoe edited this page Jul 14, 2011 · 14 revisions

The Rails Default Application Layout

This is a guide for developers using the example apps from the Rails Apps repository. Others may find it helpful as well.

If you are creating an application template, this step uses the application_layout recipe from the rails_apps_composer repository.

Background

Rails will use the layout defined in the file app/views/layouts/application.html.erb or app/views/layouts/application.html.haml as a default for rendering any page.

You’ll want to include flash messages for errors and notifications. Set up your application layout by modifying the default as described below.

Include Flash Messages

ERB

Include flash messages in app/views/layouts/application.html.erb like this:

<body>
  <%- flash.each do |name, msg| -%>
    <%= content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String) %>
  <%- end -%>
<%= yield %>

Haml

For Haml, modify app/views/layouts/application.html.haml like this:

%body
  - flash.each do |name, msg|
    = content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String)
  = yield

ERB for Rails 3.1

The Rails 3.1 modified application layout app/views/layouts/application.html.erb will look like this:

<!DOCTYPE html>
<html>
<head>
  <title>My App</title>
  <%= stylesheet_link_tag    "application" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>
<body>
  <%- flash.each do |name, msg| -%>
    <%= content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String) %>
  <%- end -%>
<%= yield %>

</body>
</html>

Haml for Rails 3.1

If you are using Haml, remove app/views/layouts/application.html.erb and replace it with app/views/layouts/application.html.haml:

$ rm public/index.html

For Haml, the Rails 3.1 modified application layout app/views/layouts/application.html.haml will look like this:

!!!
%html
  %head
    %title My App
    = stylesheet_link_tag :application
    = javascript_include_tag :application
    = csrf_meta_tags
  %body
    - flash.each do |name, msg|
      = content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String)
    = yield

ERB for Rails 3.0

The Rails 3.0 modified application layout app/views/layouts/application.html.erb will look like this:

<!DOCTYPE html>
<html>
<head>
  <title>My App</title>
  <%= stylesheet_link_tag :all %>
  <%= javascript_include_tag :defaults %>
  <%= csrf_meta_tag %>
</head>
<body>
  <%- flash.each do |name, msg| -%>
    <%= content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String) %>
  <%- end -%>
<%= yield %>

</body>
</html>

Haml for Rails 3.0

If you are using Haml, remove app/views/layouts/application.html.erb and replace it with app/views/layouts/application.html.haml:

$ rm public/index.html

For Haml, the Rails 3.0 modified application layout app/views/layouts/application.html.haml will look like this:

!!!
%html
  %head
    %title My App
    = stylesheet_link_tag :all
    = javascript_include_tag :defaults
    = csrf_meta_tag
  %body
    - flash.each do |name, msg|
      = content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String)
    = yield
Clone this wiki locally