Static pages in 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'
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
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
Url may be
http://railstech.com/page/contactus.htmlhttp://railstech.com/page/faq.html