Creating a static Rails site

August 31st, 2006 -

NOTEThis article has a space between the percentage sign and the greater than sign at the end of the code samples. This is so that it can be rendered correctly in TXP. If you are copy and pasting the code, remove the space between these two symbols.

I was recently asked how one could use Rails to create a static site, so that later, they could then change it to be dynamic or parts thereof to be dynamic. Even though Rails isn’t designed for this, it can be done quite easily.

I am going to assume that you know where you want to create your Rails directory and I will also assume you understand some of the basics of Rails. Regardless, I’ll keep this as straight forward as possible.

Just like any dynamic site you’d create in Rails, create your Rails directory – rails project_name. Next, we create a controller that will oversee our static pages – ./script/generate controller my_controller. (for windows – ruby script/generate controller my_controller)

Navigate to my_project/app/views/layouts. Create a file called application.rhtml. In this file, add your doctype, header information, and div out your site with how you’d like it to be setup. You won’t be adding any content, we’ll just be setting up the structure of your site.

In your header area, add your stylesheets and javascript files:


<%= stylesheet_link_tag "core", :media => "all" %>
<%= javascript_include_tag :defaults  %>

You can also have your page title change dynamically, in your static site by doing:


<title>
         Your Site Name <%= @page_title if @page_title %>
</title>

In your main content area, simple add:


<%= yield %>

If you have a sidebar, add:


<%= render :partial => "/my_controller/my_sidebar_view" %>

If you wanted, you could also pull out all of your main div areas into separate files in your layouts folder. You’d just call them in your application.rhtml file by doing:



<%= render :partial => "/layouts/sidebar" %>
<%= render :partial => "/layouts/content" %>

Make sure your files in the layouts folder are named: _sidebar.rhtml and _content.rhtml

Then, your _content.rhtml file you could then have the yield statement I showed above.

All partials template pages start with the underscore in the actual filename, but are called without the underscore in the actual code.

All of your content will be going in your views folder in the app folder of your application directory. So, if you wanted a home page, create a home.rthml file in your views folder (app/views/my_controller/home.rhtml). Add your content into this file and you can do the same with all of your other pages you’d like to add to your static site.

Now, in the controller we made earlier, add a method for each page that you have created, ex:

def home
  @page_title = "Home Page" 
end

def about
  @page_title = "About Page" 
end

You’ll see that I created two methods, 1) for the home page and 2) for the about page. Also, you will see that I created an instance variable called page_title and added to it a string. This defines the title of our page within our header that we created earlier. That way, when we switch pages (from home to about), you will see it reflect that.

Also, if you want to cache your static pages, you could add a before_filter in your controller:

before_filter { |c| c.cache_page }

This will cache every action of your controller. Before careful if you eventually add an authentication system to your site, because this cache the information and it will show up for every visitor of the site.

This is by no means a thorough tutorial on creating a static site with Rails, but rather a simple introduction to show how it could be done.

 

Comments

Andy

November 27th, 2006

Thanks for this! :)

The “Your Site Name…” erb code (for the title) gives a syntax error! Are you sure this code is correct?

 

Commenting has been turned off.