1

I'm trying to create a CSV file for download in Rails, and cannot get it to send just the CSV without a tag around the data. In my controller, I have:

  csv_string = CSV.generate do |csv|                                                                                                               
    headers = ['Header 1', 'Header 2']                                  
    csv << headers                                                                                                                                 
    @matches.each do |match|                                                                                                                     
      csv << match                                                                                                                                 
    end                                                                                                                                            
  end                                                                                                                                              
  send_data(csv_string, :filename => filename, :layout => false) 

The form to run this has:

=form_tag log_path, :id =>'log_search_form', :multipart => true, :remote=>true do                                                                    
    .search_fields                                                                                                                               
            .panel.panel-default                                                                                                                 
                    .panel-heading                                                                                                               
                            Search Log File:                                                                                                     
                            =file_field_tag :search                                                                                              
                            =submit_tag "Find Matches", :class=>'btn btn-primary btn-xs'                                                         

When I press "Find Matches", I am prompted to download a csv file, but the first line has:

<textarea data-type="text/csv" data-status="200" data-statusText="OK">Header 1

and the file ends with

</textarea>

The (legacy) code uses remotipart - it seems I need to stop it from overriding render and adding the textarea. How can I do this to get a clean CSV download? Thank you!

  • Did you copy pasted your `show.csv.erb` file? Because you have a typo in the first line. You have to close your array. – Uzbekjon Apr 28 '16 at 17:38
  • It looks like something is trying to display status header information, but it is being incorrectly displayed as a textarea - maybe there is code somewhere in the controller or in a shared template that is trying to explicitly issue a status header? – Fred Willmore Apr 28 '16 at 18:02
  • The csv.erb does have a close bracket - I copied wrong. I'll look for some status header code - thanks! – Shlomo Argamon Apr 28 '16 at 18:55

1 Answers1

0

I've never seen that superfluous <textarea> issue before, but try this send_data(...) parameterization, which is taken from a Rails app I maintain:

# inside your format.csv handler...
# set the filename and csv_string variables...
send_data(csv_string, :type => 'text/csv', 
                      :disposition => :attachment, 
                      :filename => filename)
chbrown
  • 11,865
  • 2
  • 52
  • 60