title: How to generate a CSV file in action?
body: <p>I am trying to generate a CSV output with data from my database. I would like to provide these data to a third party, so I picture I would give to someone a URL (<code>website.com/api_data/cars</code>) and by accessing this URL the person would be able to work with it - I think I want to access the URL and then to see there (in the action) data displayed and separated by <code>,</code> or <code>;</code>.</p>
  
  <p>But how to do that?</p>
  
  <p>So far, I am trying following approach:</p>
  
  <pre><code>csv_string = CSV.generate do |csv|
    cols = ["column one", "column two", "column three"]
    csv &lt;&lt; cols
    csv &lt;&lt; ["A", "B", "C"]
  
    @filename = "data-#{Time.now.to_date.to_s}.csv"  
  end
  send_data(csv_string, :type =&gt; 'text/csv; charset=utf-8; header=present', :filename =&gt; @filename)  
  </code></pre>
  
  <p>This is in the controller <strong>generate_data</strong> and action <strong>csv_cars</strong>.</p>
  
  <p>When I run this action (<strong>webste.com/generate_data/csv_cars</strong>), it will automatically pop up a window to download the file.</p>
  
  <p>But how to write the CSV content to the action? So when I open the URL, I'll see there written the content from the database?</p>