Shlomo Argamon
Shlomo Argamon

Reputation: 11

CSV download in Rails includes textarea tag - override Remotipart render?

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!

Upvotes: 1

Views: 160

Answers (1)

chbrown
chbrown

Reputation: 12028

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)

Upvotes: 0

Related Questions