Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a rendered view as a static file?

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?

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!

like image 459
antm Avatar asked Sep 01 '09 21:09

antm


1 Answers

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

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.

like image 137
cwninja Avatar answered Nov 01 '22 22:11

cwninja