ruby-on-rails – 如何在Ruby on Rails中下载CSV文件?
发布时间:2020-12-16 20:18:35 所属栏目:百科 来源:网络整理
导读:在我的发票控制器我有这个: def index @invoices = current_user.invoices respond_to do |format| format.html format.xls format.csv # not working! endend 在我的index.html.erb视图中,我有这两个下载链接: %= link_to "Download as Excel",invoices_p
|
在我的发票控制器我有这个:
def index
@invoices = current_user.invoices
respond_to do |format|
format.html
format.xls
format.csv # not working!
end
end
在我的index.html.erb视图中,我有这两个下载链接: <%= link_to "Download as Excel",invoices_path(:format => "xsl") %> <%= link_to "Download as CSV",invoices_path(:format => "csv") %> 模板index.xsl.erb和index.csv.erb也存在. 第一个链接工作,即Excel文件被下载到用户的计算机.但是,CSV文件在浏览器中呈现,而不是下载. 我该怎么做才能让用户下载CSV文件呢? 感谢任何帮助. 解决方法
尝试指定适当的内容标题,并将format.csv处理程序块中的index.csv.erb模板显式呈现.
# app/controllers/invoices_controller.rb
format.csv do
response.headers['Content-Type'] = 'text/csv'
response.headers['Content-Disposition'] = 'attachment; filename=invoice.csv'
render :template => "path/to/index.csv.erb"
end
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
