Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

Streaming programmatically generated content from Rails

By Peter Cooper / June 9, 2006

Rather than use the send_file or send_data methods to send an entire, completed bulk of information back to the client, David N. Welton wanted to, effectively, print stuff to the client bit by bit. He worked out how to do it and presents his findings and sample code here.

A preview of his 'output data to CSV line by line' method:

def generate_file
  @headers["Content-Type"] = "text/comma-separated-values;"
  @headers["Content-Disposition"] = "filename="some.file.txt";"

  i = 0

  render :text => Proc.new { |response, output|
    ...
    output.write("some generated text...")
    ...
  }, :layout => false
end

Other Posts to Enjoy

Twitter Mentions