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 2.0:
$ ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0]
$
Next, check if Rails 4.0 is also installed:
$ rails -v
Rails 4.0.0
$
That's looking good. If you have an older version of Ruby or Rails installed, please install the 4.0 version before you read any further.
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
      [...]
$ 

Buy the new Rails 5.1 version of this book.

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 integrated web server.

Buy the new Rails 5.1 version of this book.

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 4.0.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2013-07-15 13:58:48] INFO  WEBrick 1.3.1
[2013-07-15 13:58:48] INFO  ruby 2.0.0 (2013-06-27) [x86_64-darwin12.4.0]
[2013-07-15 13:58:48] INFO  WEBrick::HTTPServer#start: pid=98552 port=3000
The start of the Rails application is looking good. It tells us:
Rails 4.0.0 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. The log of it tells what just happend:
Started GET "/" for 127.0.0.1 at 2013-07-15 13:59:35 +0200
Processing by Rails::WelcomeController#index as HTML
  Rendered /Users/xyz/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/templates/rails/welcome/index.html.erb (1.1ms)
Completed 200 OK in 21ms (Views: 5.3ms | ActiveRecord: 0.0ms)
If we now click the link About your application's environment, we can more information on the current environment.

Buy the new Rails 5.1 version of this book.

With Ctrl+C you can stop the web server.

Static Pages

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  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      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>
<head>
  <title>Hello World!</title>
</head>
<body>
  <h1>Hello World!</h1>
  <p>An example page.</p>
</body>
</html>
Now start the Rails web server with rails server
$ rails server
=> Booting WEBrick
=> Rails 4.0.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2013-07-15 14:05:57] INFO  WEBrick 1.3.1
[2013-07-15 14:05:57] INFO  ruby 2.0.0 (2013-06-27) [x86_64-darwin12.4.0]
[2013-07-15 14:05:57] INFO  WEBrick::HTTPServer#start: pid=98564 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
No output in the log means: This page was not handled by the Rails framework. It was delivered directly from the webserver.

Buy the new Rails 5.1 version of this book.

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. Which is capable of autonomously delivering static files from the public directory. You'll learn how to set up a production webserver in Chapter 16, Web Server in Production Mode.
With Ctrl+C you can stop the Rails server.

Thank you for your support and the visibility by linking to this website on Twitter and Facebook. That helps a lot!