title: How to send a dynamically generated file in a rails app
body: <p>I'm trying to serve a dynamically generated files in a rails app, so when the user clicks a specific link, the file is generated and sent to the client using send_data.</p>
  
  <p>The file is not intended to be reused: is a short text file and regenerating should be really inexpensive as it won't be donwloaded that much; but if it is necessary or convenient I could store it in the database so is only generated once.</p>
  
  <p>First, I would like to generate the file in memory, and send it in the controller. I'm trying to archive something like this:</p>
  
  <pre><code>def DownloadsController &lt; ApplicationController
    def project_file
      project = Project.find(params[:id])
      send_data project.generate_really_simply_text_file_report
    end
  end
  </code></pre>
  
  <p>But i don't know how to generate a stream in memory, so no file is created in the file system.</p>
  
  <p>Another option would be generating the file with a random name in the rails app tmp directory and send it from ther, but then the file will be kept there, which is something I would prefer not to happen.</p>
  
  <p>Edit: If I'm not mistaken, send_file blocks the petition until the file is sent, so it could work...</p>
  
  <p>Any other advices or opinions?</p>
  
  <p>Thanks in advance</p>