Rails 如何套用 erb 模板生成 html 页面并写入文件?

alucardpj · March 31, 2012 · Last by ywencn replied at March 31, 2012 · 7436 hits

我有一个需求,要抓取另一个应用接口提供的 xml 数据,然后组装成 html 页面打包做成离线资源包,现在需要将 xml 数据解析后套用预置的 html 模板生成 html 页面然后写到文件里去保存,该如何操作?使用 render?平时只是用 render 渲染页面输出而不是保存到文件。

erb 本身是可以在 rails 外操作的,看看

http://ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB.html

最简单的办法,在 action 里加个 http header response.headers['Content-Disposition'] = 'attachment;filename=filename.html'

You need to Sign in before reply, if you don't have an account, please Sign up first.