蕲春人的博客

About Blog Email GitHub

06 Apr 2010
Static site using rails

原文地址: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"
    }
end
  • Step 4:- Add following line in routes.rb

    map.page “page/:filename.:format”, :controller => ‘static_pages’, :action => ‘page’

  • Step 5:- Now add following line into static_pages controller

def page
 send_file
 "#{Rails.root}/app/views/static_pages/#{params[:filename]}.#{params[:format]}",:disposition =>'inline',:type => StaticPage::Formats[params[:format]]
 end
  • 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

蕲春人 at 09:12

scribble

About Blog Email GitHub