Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails link_to download file

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

like image 965
Stiven Frams Avatar asked Aug 21 '16 23:08

Stiven Frams


1 Answers

You can do steps as below:

Step1: Open file routes.rb

get 'download_pdf', to: "homes#download_pdf"

Step2: I assumed your controller was home_controller.rb, you put this line:

def download_pdf
  send_file "#{Rails.root}/app/assets/docs/Физика.pdf", type: "application/pdf", x_sendfile: true
end

Step3: In your view file.

<%= link_to "download", download_pdf_path %>

I suggest that you should put this docs folder in public folder.

For example: public/docs/*.pdf

like image 154
Khanh Pham Avatar answered Nov 08 '22 14:11

Khanh Pham