title: Export A Record To CSV
body: <p>I have a Posts table in my Rails 3.0.10 app. I want to give my users the option to export a particular Post record to CSV format, not all of them. And while my Post table has a lot of fields, I only want to export the title and the body.</p>
  
  <p>After doing some searching apparently the best way to do this is through FasterCSV. And apparently it's already built in Ruby 1.9.2, which I'm using. Thing is pretty much all the tutorials are outdated (from Rails 1 or 2) and I have absolutely no idea how to accomplish this.</p>
  
  <p>I've tried putting in my posts_controller.rb</p>
  
  <pre><code>def export_to_csv 
    @post = Post.find(params[:id])
       csv_string = CSV.generate do |csv| 
      csv &lt;&lt; [@post.title, @post.body] 
    end 
  
  # send it to the browsah
  send_data csv_string, 
            :type =&gt; 'text/csv; charset=iso-8859-1; header=present', 
            :disposition =&gt; "attachment; filename=post.csv" 
  end
  </code></pre>
  
  <p>Which I <em>THINK</em> may be the right code, but I have no idea how to use it in my view. Ideally I want to have a link to export the CSV file, but I'm thinking it has to be done through a form_tag?</p>
  
  <p>Would appreciate if someone could point me towards the right direction. Thanks.</p>