3.2. Static Content (HTML and Graphics Files)

If you are reading this text, you will already know that you can use Rails to somehow output web pages. The question is just how it's done. Let's first create a new Rails project.

Create Rails Project

Before we even get going, please check that you are using Ruby version 1.9.3:
$ ruby -v
ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin12.2.1]
$
Next, check if Rails 3.2 is also installed:
$ rails -v
Rails 3.2.9
$
That's looking good. If you have an older version of Ruby or Rails installed, please install the current version before you read any further (see Chapter 1, Version Selection and Installation).
Now we start by creating a new Rails project with the name testproject. Ruby on Rails is a framework, so we first need to set up the corresponding directory structure and basic configuration, including several scripts. Easy as pie, just use the command rails new testproject:
$ rails new testproject
      create  
      create  README.rdoc
      create  Rakefile
      create  config.ru

      [...]

$ 
In previous versions of Rails, you had to do a bundle install first. With Rails 3.2 onwards, this is done automatically when you create a new Rails project. The Rails project then has all required gems available. A gem is a kind of software library. You can integrate certain ready-made functionalities with it, without having to reinvent the wheel.
Next, we check if the new Rails application is working by launching the little integrated web server.

Tip

Depending on the operating system (for example, Mac OS X) and on your firewall settings, you may see a popup window when first starting a Rails application, asking you if the firewall should permit the corresponding connection. As we are only working locally, you can safely confirm.
$ cd testproject 
$ rails server
=> Booting WEBrick
=> Rails 3.2.9 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-11-13 18:48:39] INFO  WEBrick 1.3.1
[2012-11-13 18:48:39] INFO  ruby 1.9.3 (2012-11-10) [x86_64-darwin12.2.1]
[2012-11-13 18:48:39] INFO  WEBrick::HTTPServer#start: pid=73258 port=3000
The start of the Rails application is looking good. It tells us:
=> Rails 3.2.9 application starting in development on http://0.0.0.0:3000
So let's go to the URL http://0.0.0.0:3000 or http://localhost:3000 in the web browser.
Default index page in a new Rails project.
Looks good. Rails seems to be working fine. If we now click the link About your application's environment, we can get information on the current environment.
About the start environment of the Rails application.
At the same time, the log of the web server will display this:
Started GET "/rails/info/properties" for 127.0.0.1 at 2012-11-13 18:50:57 +0100
Processing by Rails::InfoController#properties as */*
  Rendered inline template (23.5ms)
Completed 200 OK in 35ms (Views: 34.6ms | ActiveRecord: 0.0ms)
When we accessed the first start page, no such entry appeared, so there must be two different pages. The first page is in fact a static HTML page stored under public/index.html, and the link we clicked executes a Rails program.
With Ctrl+C you can stop the web server again.

Static Pages

As we have just seen, there are certain static pages, images and JavaScript files that are automatically output by Rails. Remember part of the output of the command rails new testproject:
$ rails new testproject
      create  

      [...]

      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/index.html
      create  public/robots.txt

      [...]
The directory name public and the files it contains already look very much like static pages. Let's have a go and create the file public/hello-world.html with the following content:
<html>
<title>Hello World!</title>
<body>
  <h1>Hello World!</h1>
  <p>An example page.</p>
</body>
</html>

Note

I assume that you have rudimentary knowledge of HTML.
Now start the Rails web server:
$ rails server
=> Booting WEBrick
=> Rails 3.2.9 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-11-13 18:58:17] INFO  WEBrick 1.3.1
[2012-11-13 18:58:17] INFO  ruby 1.9.3 (2012-11-10) [x86_64-darwin12.2.1]
[2012-11-13 18:58:17] INFO  WEBrick::HTTPServer#start: pid=73455 port=3000
We can have a look at this web page at the URL http://0.0.0.0:3000/hello-world:
Hello World HTML page
We can of course also use the URL http://0.0.0.0:3000/hello-world.html. But Rails regards HTML and therefore the file ending .html as standard output format, so you can omit the .html here.
Now you know how you can integrate fully static pages in Rails. This is useful for pages that never change and that you want to work even if Rails is not currently working, for example because of an update. In a production environment, you would usually put a classic web server such as Apache or Nginx infront of the Rails server. This is capable of autonomously delivering static files from the public directory. You'll learn how to set up a production webserver in Chapter 15, Web Server in Production Mode.
With Ctrl+C you can stop the Rails server again.

Updates about this book will be published on my Twitter feed.