原文地址: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
Step 2:-Generate StaticPage Controller
1
ruby script/generate controller static_pages page
Step 3:- Create StaticPage Class in Model
1
2
3
4
5
6
7
class StaticPage
Formats = {
"html" => "text/html" ,
"png" => "image/png" ,
"jpg" => "image/jpg"
}
end
Step 4:- Add following line in routes.rb
1
map . page "page/:filename.:format" , :controller => 'static_pages' , :action => 'page'
Step 5:- Now add following line into static_pages controller
1
2
3
4
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.