开发者社区> 问答> 正文

将CSV呈现链接为HTML

我的rails应用程序中有一个链接,应该导出CSV。但是当我点击链接时,它会将页面重新加载为html。我尝试添加response.headers['Content-Type'] = 'text/csv'

response.headers['Content-Disposition'] = 'attachment; filename=invoice.csv'到控制器,但这没有帮助。

调节器
def index

respond_to do |format|

format.html
format.csv do
  response.headers['Content-Type'] = 'text/csv'
  response.headers['Content-Disposition'] = 'attachment; filename=custome_incentives.csv'
  send_data @incentives.to_csv
end

end
end
ERB
<%= link_to 'Download to CSV', incentives_path(property.slug, format: :csv), class: 'btn csv' %>

展开
收起
小六码奴 2019-04-10 10:55:10 1987 0
1 条回答
写回答
取消 提交回答
  • 假设路线都处于良好状态,您是否尝试过这种方式包括filename这样的?

    def index

    respond_to do |format|

    format.html
    format.csv do
      send_data @incentives.to_csv, filename: 'custome_incentives.csv'
    end

    end
    end
    如果不能解决问题,也可以强制下载链接。

    2019-07-17 23:33:05
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
《零基础HTML入门教程》 立即下载
天猫 HTML5 互动技术实践 立即下载
天猫HTML5互动技术实践 立即下载