rails link_to download file
Asked Answered
R

9

10

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

Rile answered 21/8, 2016 at 23:53 Comment(3)
try this link.Amaurosis
Maybe just <%= link_to 'download', '/assets/docs/Физика.pdf' %> or <%= link_to 'download', asset_path('/docs/Физика.pdf') %>Internship
@Internship your answer is the correct answerClop
D
9

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

Diuretic answered 22/8, 2016 at 2:3 Comment(1)
Worked smoothly on Rails 7.Kinchen
M
5

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
Malvasia answered 22/8, 2018 at 7:32 Comment(0)
I
2

When placing files in /assets you can use the Rails helper #asset_path.

<%= link_to 'download', asset_path('/docs/Физика.pdf') %>

source: http://guides.rubyonrails.org/asset_pipeline.html#asset-organization

Internship answered 22/8, 2016 at 0:49 Comment(0)
C
2

Oddly enough, using the HTML download attribute in your link_to helper does the trick

<%= link_to "Download", file.file(:original, false), download:true %>

Hope this helps in the future!

Culminate answered 6/10, 2017 at 20:14 Comment(1)
you should pass filename to "download", i. e. download: file.filename instead of download: trueEnterotomy
R
1

Try this:

<%= link_to 'download', root_path << '/assets/docs/Физика.pdf' %>
Rosaline answered 21/8, 2016 at 23:59 Comment(2)
Did not help( No route matches [GET] "/assets/docs/%D0%A4%D0%B8%D0%B7%D0%B8%D0%BA%D0%B0.pdf"Rile
@StivenFrams did you get the solution ? I am facing the same error.Szymanski
L
1

What works for me and also was the easiest:

= link_to "Click to download", asset_path("logo.png"), download: true
Landaulet answered 28/12, 2020 at 21:43 Comment(1)
I will give upvotes if its not on haml or slim but htmlClop
I
0

The documentation indicates how build a download link to attachment file, would be like this

<a href="<%= user.avatar.attached? ? rails_blob_path(user.avatar, disposition: 'attachment') : '#' %>" target="_blank" download>Link</a>
Inearth answered 10/3, 2020 at 22:41 Comment(0)
F
0

Rails 6 solution:

Step 1: Create your download route in your routes.rb file:

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

Step 2: Add the link to your views:

<%= link_to "download", download_single_path(url: 'url', file_name: 'filename') %>

Step 3: Add the action in your Controller homes_controller.rb where you take the params that you pass in your link_to:

def download_pdf
  require 'open-uri'
  url = params[:url]
  file_name = params[file_name]
  data = open(url).read
  send_data data, :disposition => 'attachment', :filename=>"#{file_name}.pdf"
end
Filberte answered 12/10, 2021 at 8:38 Comment(0)
C
0

try to do this:

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

Pass the whole path, and include download between curly brackets

Circumjacent answered 3/7 at 6:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.