Ruby on rails 如何在RubyonRails中设置对虾pdf文件名?

Ruby on rails 如何在RubyonRails中设置对虾pdf文件名?,ruby-on-rails,pdf-generation,prawn,Ruby On Rails,Pdf Generation,Prawn,我看到的是控制器对html和pdf文件格式的响应,如下所示: def detail @record = Model.find(params[:id]) respond_to do |format| format.html # detail.html.erb format.pdf { render :layout => false } #detail.pdf.prawn end end 但是当我得到这个文件

我看到的是控制器对html和pdf文件格式的响应,如下所示:

   def detail
      @record = Model.find(params[:id])
      respond_to do |format|
         format.html # detail.html.erb
         format.pdf { render :layout => false } #detail.pdf.prawn
      end
   end
但是当我得到这个文件时,它的名字是:
1.pdf
2.pdf
这取决于
params[:id]
如何将文件名设置为
myfile.pdf

--更新--

我的
detail.pdf.prawn
文件示例


pdf.font "Helvetica"
pdf.image open("http://localhost:3000/images/myImage.png"),:position => :left,:width=>100
pdf.text "some text"
pdf.table(someData,:cell_style => { :border_width => 0.1,:border_color=> 'C1C1C1' }) do |table|
    table.row(0).style :background_color => 'D3D3D3'
    table.column(0..1).style(:align => :left)
    table.column(2..4).style(:align => :center)
    table.column(0).width = 100
    table.column(1).width = 250
    table.column(3..4).width = 68
    table.row(2).column(0..2).borders = []
    table.row(2).column(3).style(:font_style => :bold, :align => :right)
end

控制器中的
format.pdf{render:layout=>false}
呈现de pdf文件,其中包含
detail.pdf.prawn

说明fl00r的答案,如果您使用的是prawnto,则pdf设置参数可以进入控制器,包括文件名

prawnto :filename => "myfile", :prawn => {
  ...
}
def detail
 @record = Model.find(params[:id])
 prawnto :prawn => { :page_size => 'A4', 
                     :left_margin => 50,    
                     :right_margin => 50,   
                     :top_margin => 80,    
                     :bottom_margin => 50}, 
             :filename => @record.name, :inline => true #or false


      respond_to do |format|
         format.html # detail.html.erb
         format.pdf { render :layout => false } #detail.pdf.prawn
      end
   end
如果您正在使用prawnto创建大量不同的pdf,您可能会将配置移到它自己的方法中。但是如果你只做一个,在控制器中就可以了


注意:PDF url仍将显示,例如1.PDF,但当他们保存PDF时,文件名参数将显示在“保存”对话框中。

非常感谢,这篇文章确实帮助了我的旧PDF方式。还有另一种方法可以使用Rails的
prawn
。你可以看看这个。不是我的,而是制作它的好教程。只是说可能对那些仍然困惑如何制作的人来说


我以前使用过这个方法,然后从那个链接继续使用这个方法。做一些关于它的研究真的很有趣。

我应该在视图
detail.pdf.prawn上做这件事,还是应该在控制器
detail
操作中做这件事?检查这个问题的更新我添加了
detail.pdf.prawn
文件的内容获取这个错误
语法错误,意外=>,应为关键字\u end prawnto:prawn=>:filename=>@hotel\u shell.address.hotel\u code
找到它。基本上必须添加
{:page\u size=>'A4'},