在Rails中部署静态网页

Tuesday, June 15, 2010

我们知道rails主要是用来写动态网站, 但也可以处理静态网页或者部署完全静态的网站

1, 新建一个rails项目

rails static_site

2, 创建一个控制器

ruby script/generate controller static_pages page

3, 在模型中创建一个StaticPage类

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

4, 增加路由

map.page "page/:filename.:format", :controller => 'static_pages', :action => 'page'
like http://sitename/page/static_filename.html

5,控制器

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

6, 把所有的静态网页放RAILS_ROOT/app/views/static_pages/文件夹下

7, 启动服务ruby script/server

译自:http://railstech.com/?p=89

This entry was tagged Rails

comments powered by Disqus

© 2009-2013 lxneng.com. All rights reserved. Powered by Pyramid

go to Top