简体   繁体   English

Rails中的CSV下载包括textarea标签 - 覆盖Remotipart渲染?

[英]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. 我正在尝试创建一个CSV文件以便在Rails中下载,并且不能让它只发送没有数据标签的CSV。 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: 当我按“查找匹配”时,系统会提示我下载csv文件,但第一行有:

<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. (遗留)代码使用remotipart - 似乎我需要阻止它覆盖渲染并添加textarea。 How can I do this to get a clean CSV download? 如何才能获得干净的CSV下载? Thank you! 谢谢!

I've never seen that superfluous <textarea> issue before, but try this send_data(...) parameterization, which is taken from a Rails app I maintain: 我之前从未见过那个多余的<textarea>问题,但尝试这个send_data(...)参数化,它取自我维护的Rails应用程序:

# inside your format.csv handler...
# set the filename and csv_string variables...
send_data(csv_string, :type => 'text/csv', 
                      :disposition => :attachment, 
                      :filename => filename)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM