大虾rails 下载就打开

prawn rails download on open

我用prawn-rails.

我有 show.pdf.prawn 观点。

我想执行下载此 pdf 的操作。

我尝试了以下方法:

    def download
      send_data(
        show,
        filename: "#{custom_name}.pdf",
        type: 'application/pdf'
      )
    end

它的作用是下载一个名为 #{custom_name}.pdf 的东西,但我无法打开它 (falied to load PDF document)。

改成

    def download
      send_file(
        show,
        filename: "#{custom_name}.pdf",
        type: 'application/pdf'
      )
    end

我得到

no implicit conversion of true into String

请指教

怎么样:

def download
  response.headers['Content-Disposition'] = 'attachment; filename="' + custom_name + '.pdf"'
  render :show, mime_type: Mime::Type["application/pdf"]
end

应该有效:

send_data render_to_string("show"), filename: "#{custom_name}.pdf",
                                    type: 'application/pdf'

您也可以使用 prawnto_2 库:

gem "prawnto_2",  require: "prawnto"

然后简单地在控制器动作中渲染:

def show
  respond_to do |format|
    format.pdf  { 
        prawnto inline:   false,
                filename: "#{custom_name}.pdf",
                prawn:    {} # prawn settings here
    }
  end
end

但是我不确定这个库目前是如何维护的,但是仍然可以使用 Rails 4.2.x.