12

我在使用 Rails 方法时遇到问题:send_data

这是我的行动:

def export
  send_data(current_user.contacts.to_csv,
    type: 'text/csv; charset=utf-8; header=present',
    disposition: 'attachment; filename=contacts.csv'
  )
end

这不会提示下载,它只是在屏幕上呈现结果。powthin服务器我都试过了。

我无法弄清楚我做错了什么?

我在用着rails 4.0.0.beta

编辑:

卷曲标题:

< HTTP/1.1 200 OK
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< X-UA-Compatible: chrome=1
< X-XHR-Current-Location: /contacts/export
< Content-Disposition: attachment; filename=contacts.csv
< Content-Transfer-Encoding: binary
< Content-Type: text/csv; charset=utf-8; header=present
< Cache-Control: private
< ETag: "48d3d8bd1c8d25cafb82ab705e4875ab"
< Set-Cookie: request_method=GET; path=/
< X-Request-Id: c2588883-f3f9-4f68-8a8c-0de758c47288
< X-Runtime: 0.185206
< Connection: close
< Server: thin 1.5.0 codename Knife
4

3 回答 3

12

这里的答案适用于 turbolinks classic。较新版本的 turbolinks 有一个较新的符号:

<a href="/" data-turbolinks="false">Disabled</a>

https://github.com/turbolinks/turbolinks#disabling-turbolinks-on-specific-links

于 2016-05-09T13:12:47.373 回答
9

我想到了。

是涡轮链接把这一切搞砸了。我将 data-no-turbolink 添加到导出链接,现在它按预期工作。

于 2013-02-12T07:18:38.950 回答
2

send_data有一个选项散列,因此类型、处置和文件名需要在散列中设置:

def export
  send_data(current_user.contacts.to_csv,
  type: 'text/csv', disposition: 'attachment', filename: 'contacts.csv')
end
于 2013-02-11T18:26:58.290 回答