使用Ruby on Rails导出csv文件在safari中不起作用

时间:2017-11-09 04:52:35

标签: ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-3.2 ruby-on-rails-5

我需要将数据导出为csv格式。下面的代码在firefox中工作正常,但没有像预期的那样在safari中工作。

report.html.erb

<%= link_to 'Download CSV', reports_path(format: :csv), class: 'white' %>

report.csv.erb

<%- headers = [' Date ',   ' Type ',   'Amount ',    ' Amount '] -%>
<%= CSV.generate_line headers %>
<% if @reports.present? %>
<%- @reports.each do |report| -%>
<%= CSV.generate_line([report['Date'],
                       report['Type'],
                       report['Amount'],
                       report['Amount']]).html_safe -%>
<%- end -%>
<% end %>

reports_controller.rb

respond_to do |format|
      format.html
      format.csv do
        log('info', current_user.email, 'File creating for Cash book..')
        headers['Content-Disposition'] = 'attachment; filename="filename"'
        headers['Content-Type'] ||= 'text/csv'
      end
    end

有人可以帮忙吗?

0 个答案:

没有答案