我正在生成PDF文件,我的链接如下所示:
<%= link_to 'Invoice',display_invoice_path(invoice.id),:format => :pdf %>
当我点击它时,它将我带到/ display_invoice / 123456789(这是一个HTML版本).
在控制器中,操作如下:
def display_invoice if params[:invoice_number] @invoice = ... respond_to do |format| format.html format.pdf do #render pdf: '123',# file name render pdf: params[:invoice_number],layout: 'layouts/application.pdf.erb'#,# layout used #show_as_html: params[:debug].present? # allow debuging end end end end
并在路线:
get '/display_invoice/:invoice_number',to: 'invoices#display_invoice',:as => 'display_invoice'
点击链接后,我想要在网址/display_invoice/INVOICE_NUMBER.pdf中显示 – 目前只有/ display_invoice / INVOICE_NUMBER.
如何用“.pdf”后缀打开它?
谢谢.
解决方法
您需要添加pdf mime类型.
将以下行添加到文件config / initializers / mime_types.rb:
Mime::Type.register "application/pdf",:pdf
有关详情,请参见http://guides.rubyonrails.org/action_controller_overview.html#restful-downloads.
编辑:
格式需要是路径助手的一部分:
display_invoice_path(invoice.id,:format => :pdf)