rails link_to download file

Stiven Frams picture Stiven Frams · Aug 22, 2016 · Viewed 17.4k times · Source

I need to add a link to download the file from assets/docs/Физика.pdf I do not know how to do that. I am trying to do so here: in view -

<%= link_to "download", '/Физика.pdf', :download => 'filename' %>

I get an error message:

No route matches [GET] "/%D0%A4%D0%B8%D0%B7%D0%B8%D0%BA%D0%B0.pdf"

What am I doing wrong? Help me please

Answer

vidur punj picture vidur punj · Aug 22, 2018

step 1: The View

<%= link_to "download", download_path, target: "_blank"%>

step 2: Routing

match 'download', to: 'home#download', as: 'download', via: :get

step 3: Inside controller

send_file 'public/pdf/user.png', type: 'image/png', status: 202