Rails 4 - how to make a link to a PDF file (name.PDF)?
Asked Answered
B

3

9

I am generating PDF files and my link look like this:

<%= link_to 'Invoice', display_invoice_path(invoice.id), :format => :pdf %>

When I click on this, it takes me to /display_invoice/123456789 (it's an HTML version).

In the controller action is following:

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

and in the routes:

  get '/display_invoice/:invoice_number', to: 'invoices#display_invoice', :as => 'display_invoice'

After clicking the link, I'd like to have in the URL /display_invoice/INVOICE_NUMBER.pdf - currently, there's just /display_invoice/INVOICE_NUMBER.

How to open it with the ".pdf" suffix?

Thank you.

Benedicite answered 26/8, 2014 at 9:25 Comment(1)
Try to add :format => :pdf into the display_invoice_path helper like display_invoice_path(invoice, :format => :pdf)Escarole
P
12

You need to add the pdf mime type.

Add the following line to the file config/initializers/mime_types.rb:

Mime::Type.register "application/pdf", :pdf

See http://guides.rubyonrails.org/action_controller_overview.html#restful-downloads for details.

EDIT:

The format needs to be part of the path helper:

display_invoice_path(invoice.id, :format => :pdf)
Prevention answered 26/8, 2014 at 9:39 Comment(4)
I've added this to the mentioned file. Btw, when I manually put to the URL the PDF suffix, like /display_invoice/INVOICE_NUMBER.pdf, then the PDF file is properly generated, but the problem is that I don't know how to set up the link_to for displaying the PDF file.Benedicite
I updated my answer, you need to include the format in the path helper.Prevention
why I'm getting a warning: already initialized constant Mime::PDF and warning: previous definition of PDF was here. Is it already loaded in Rails 4 > ?Goal
@Goal what happens if you omit the mime type registration? If it still works you can assume that it's already loaded somewhere else. This can be either rails itself, or a gem, or somewhere else in your code.Prevention
B
3

use this code instead of your code

<%= link_to 'Invoice', display_invoice_path(invoice.id, :format => :pdf) %>
Below answered 26/8, 2014 at 10:39 Comment(2)
Please explain what is wrong with OP's code and why this solves the problem.Vyky
Difference is GET and POSTBelow
E
0

There is a new syntax without using Hash Rocket

<%= link_to "Invoice", display_invoice_path(invoide, format: :pdf), target: "_blank" %>

If you want to have URL in anchor tag then use .._url instead of .._path

<%= link_to "Invoice", display_invoice_url(invoide, format: :pdf), target: "_blank" %>
Ezaria answered 22/7, 2020 at 18:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.