Rails 4 - how do I link to a PDF file (name.PDF)?

I am creating PDF files and my link looks like this:

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

When I click on this I need / display_invoice / 123456789 (this is the HTML version).

The controller action is as follows:

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 would like to have the URL /display_invoice/INVOICE_NUMBER.pdf - currently just / display_invoice / INVOICE_NUMBER .

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

Thank.

+4
source share
2 answers

You need to add the mime pdf type.

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

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

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

EDIT:

:

display_invoice_path(invoice.id, :format => :pdf)
+6

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

All Articles