Feb 10

Static site using rails

As we know rails is mainly used for dynamic website.we can also display static web pages or we can deploy full static website using rails. The following code can help us to display static pages.

Step 1:-Create Rails project

rails static_site

Step 2:-Generate StaticPage Controller

ruby script/generate controller static_pages page

Step 3:- Create StaticPage Class in Model

 class StaticPage
   Formats = {
       "html" => "text/html",
       "png" => "image/png",
       "jpg" => "image/jpg"

Step 4:- Add following line in routes.rb

map.page "page/:filename.:format", :controller => 'static_pages', :action => 'page'

Here we are passing filename as parameter which is static file name.

This will generate url as http://sitename/page/static_filename.html

Step 5:- Now add following line into static_pages controller

def page
 "#{Rails.root}/app/views/static_pages/#{params[:filename]}.#{params[:format]}",:disposition =>'inline',:type => StaticPage::Formats[params[:format]]

Step 6:- All the static pages place in RAILS_ROOT/app/views/static_pages/ folder

Step 7 :- Start server and Type url as shown below.

ruby script/server

Url may be

http://railstech.com/page/contactus.html or
Or In Development Mode

Be Sociable, Share!

6 Responses to “Static pages in rails”

  1. Atul Agrawal says:

    Quite useful to add pages like about us, contact us to rails sites.

  2. This is a good method to generate your content in rails, and you can even take it one step further by using apache & page caching.

    I find best place to serve static pages up is directly from Apache. With this method, rails is still being hit with requests, and in my experience limits your throughput to 40-60 requests/sec. Apache on the other hand can serve static content at several thousand requests/sec.

    If you still want to hit rails to generate your content, I suggest cache_page to generate content in your /public/ directory. To get rid of cache pages easily, look up cache_sweeper.

    To serve these pages directly from Apache, set your DocumentRoot to your rails projects public directory, and use ModRewrite to serve pages from your cache/static directory.

  3. Lenary says:

    You know, this is exactly what the public folder is for – just put the about.html file in public/ and then you can serve it at example.com/about.html

    it’s pretty stupid to use a controller as that goes through the whole rails stack. instead, if you use a public folder then yopu can get apache to serve them in when in production, which will be far faster than your rails stack.

    @Butch i would say rewriting to your cache directory was a bad way to go. instead just cache them directly to the public directory.

  4. admin says:

    I know,but recently i am working on one of the project where we have to display office format file in html.so when any office format file is uploaded by user i am converting to an html format.and this file is not to be place in a public folder.so i want to display those static pages.on that way this post is useful.
    thanks for replying.

  5. most-na says:

    public folder does not render the pages in the application’s layout. using an action for static pages does this nicely 😉

Leave a Reply

preload preload preload