如何将渲染视图保存为静态文件? [英] How to save a rendered view as a static file?

查看:32
本文介绍了如何将渲染视图保存为静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

My Rails 2.3 应用程序以 HTML/CSS 或 Word 文档的形式生成页面.我想将该 html 文件作为静态文件(即 filename.html 或 filename.doc)保存到文件系统中.我计划有一个带有完全呈现页面和保存报告"按钮的预览操作.我们的用户稍后将访问这些静态文件.(我会保存数据库的路径.)

My Rails 2.3 app generates a page in HTML/CSS or as a word doc. I'd like to save that html file to the filesystem as a static file (ie. filename.html or filename.doc). I plan to have a preview action w/ the fully rendered page and a 'save report' button. Our users will access those static files later. (I'll save the path to the db.)

有什么关于如何做到这一点的建议吗?

Any suggestions for how to do this?

我正在创建一个文件并保存它,但我不确定如何将渲染视图放入其中.如果有人知道如何将其保存到 S3,则可以获得奖励积分!非常感谢!

I'm as far as creating a file and saving it, but I'm not sure how to get my rendered view into it. Bonus points if anyone knows how to save it up to S3! Many thanks!

推荐答案

render_to_string 是你的朋友.一个你把它放在一个字符串中,以通常的方式将它刻录到文件中.

render_to_string is your friend. One you have it in a string, burn it to file in the usual way.

class FooController
  def save_foo_to_disk
    data = render_to_string( :action => :index )
    File.open(file_path, "w"){|f| f << data }
    flash[:notice] = "saved to #{file_path}"
  end
end

就 S3 而言,请参阅 aws-s3 gem.它似乎在做你所追求的.用法有点像这样.

As far as S3 goes, see the aws-s3 gem. It seem to do what you are after. Usage is a little like this.

AWS::S3::Base.establish_connection!(
  :access_key_id     => 'abc',
  :secret_access_key => '123'
)
S3Object.store(file_name, data, 'bucket-name')

玩得开心,不要拿着剪刀跑.

Have fun, and don't run with scissors.

这篇关于如何将渲染视图保存为静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆