Rails send_data返回HTTP错误406

时间:2013-08-29 09:46:04

标签: ruby-on-rails ruby http download xls

我在数据库中作为XLS导出时遇到了一些问题。 我使用了gem to_xls,在下面的文档中我有这个错误,我不知道如何获取并启动我的文件下载:

控制器:

def export
  @subscribes = Subscribe.all
  respond_to do |format |
    format.xls { send_data@subscribes.to_xls, disposition: 'attachment', : filename = > 'Subscribes.xls' }
  end
end

我的MIME文件

Mime::Type.register "application/vnd.ms-excel", :xls

所以,我用完整标题得到的错误:

Request URL:https://localhost:3000/subscribes/export
Request Method:GET
Status Code:406 Not Acceptable

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Host:localhost:3000
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36

Cache-Control:no-cache
Connection:close
Content-Type:text/html; charset=utf-8
Server:thin 1.5.1 codename Straight Razor
Strict-Transport-Security:max-age=31536000
X-Request-Id:a5be7889994f1035818d6a554f991636
X-Runtime:0.012001
X-UA-Compatible:IE=Edge

感谢您帮助我:)

1 个答案:

答案 0 :(得分:1)

由于您使用的是format.xls,因此您必须将.xls附加到网址。尝试

curl -v https://localhost:3000/subscribes/export.xls

否则,您可以将send_data移出respond_to阻止。