我需要将数据导出为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
有人可以帮忙吗?