Creating A Layout

Important files:

  • app/views/layouts/application.html.erb: This will be the default template used to layout all your web pages.
  • public/stylesheets/application.css: This will be our default css file. It does not have to be called application.css. We call it that because of the line stylesheet_link_tag 'application' in the layout.

application.html.erb

codetitle. app/views/layout/application.html.erb

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  <title><%= controller.action_name %></title>
  <%= stylesheet_link_tag 'application' %>
</head>
<body>

<p style="color: green"><%= flash[:notice] %></p>

<%= yield  %>

</body>
</html>

A couple things to note:

  • the character encoding of your application will be UTF-8. This is a very good idea, although it will not really be true UTF-8 unless your database is also UTF-8.
  • The yield command will insert the result of the current view into this template.
  • The doctype is set to xhtml. This makes it so that the browser will be less forgiving if you include badly formatted html. In short, you must make sure that every time you open an html tag that you also close it!

application.css

nothing here yet.