rails excel mime-type - 如何更改默认文件名?

tma*_*ini 4 ruby-on-rails railscasts mime-types

我按照http://railscasts.com/episodes/362-exporting-csv-and-excel进行操作, 并在我的Rails应用程序中设置Excel下载.

我的控制器代码如下所示:

  def show
    @project = Project.find(params[:id])
    @project.tasks.order(:name)
    respond_to do |format|
      format.html
      format.json { render json: @project }
      format.xls
    end
  end
Run Code Online (Sandbox Code Playgroud)

在我看来,我创建了下载excel文件的链接,如下所示:

.dl_xls= link_to "Download xls", url_for(:format => 'xls')
Run Code Online (Sandbox Code Playgroud)

现在,生成的excel文件总是被命名为Project记录的id ,例如80.xls

有没有办法改变这种行为并给它一个自定义名称?

谢谢..

cut*_*ion 24

我相信你的答案就在这里:在rails中,如何将记录作为csv文件返回

使用标头设置文件名.

headers["Content-Disposition"] = "attachment; filename=\"#{filename}\"" 
Run Code Online (Sandbox Code Playgroud)


akb*_*bin 6

def index
  @tabulars = Tabular.all
  filename = "data_users.xls"
  respond_to do |format|
    format.html
    format.xls { headers["Content-Disposition"] = "attachment; filename=\"#{filename}\"" }
  end
end
Run Code Online (Sandbox Code Playgroud)

此链接更详细地更改了文件名excel