How to save a rendered view as a static file?

后端 未结 2 742
太阳男子
太阳男子 2021-01-31 11:43

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 t

相关标签:
2条回答
  • 2021-01-31 12:33

    Another way is adding an after_action to the controller, and in that action using response.body to access rendered content. In this way, your controller can respond to client as normal, save rendered content to database in meanwhile.

    0 讨论(0)
  • 2021-01-31 12:44

    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.

    0 讨论(0)
提交回复
热议问题