Rails 3 - 在 respond_to 中设置文件名 [英] Rails 3 - set the filename in a respond_to
问题描述
这看起来应该很简单,但我似乎找不到直接的答案.
This seems like it should be simple, but I can't seem to find a straight answer.
我添加了一个 csv mime 类型,以下似乎有效,除了下载的文件总是命名为report.csv".
I have added a csv mime-type, and the following seems to work, except that the downloaded file is always named "report.csv".
在我的控制器中:
def report
respond_to do |format|
format.html
format.csv do
render :template => "summary/report.csv.erb",
:filename => "foo" #doesn't work
end
end
end
我认为它使用的是默认渲染器(我还没有实现替代渲染器),但我似乎无法找到有关可用选项的完整文档.
I think it's using the default renderer (I haven't implemented an alternate renderer), but I can't seem to find complete docs on the options available.
难道没有类似文件名"选项或我可以使用的东西吗?有没有更好的方法?
Isn't there something like a "filename" option or something that I can use? Is there a better approach?
推荐答案
我明白了,感谢 this answer一>.
format.csv do
response.headers['Content-Disposition'] = 'attachment; filename="' + filename + '.csv"'
render "summary/report.csv.erb"
end
首先在响应头中设置文件名,然后调用render
.
First you set the filename in the response header, then you call render
.
(render
的模板参数是可选的,但在我的情况下我需要它.)
(The template param to render
is optional, but in my case I needed it.)
这篇关于Rails 3 - 在 respond_to 中设置文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!