Reputation: 127
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
Upvotes: 10
Views: 26714
Reputation: 309
try to do this:
<%= link_to "download", '/assets/docs/Физика.pdf', { download: 'filename' } %>
Pass the whole path, and include download between curly brackets
Upvotes: 0
Reputation: 409
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
Upvotes: 0
Reputation: 727
What works for me and also was the easiest:
= link_to "Click to download", asset_path("logo.png"), download: true
Upvotes: 1
Reputation: 429
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>
Upvotes: 0
Reputation: 5901
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
Upvotes: 5
Reputation: 1123
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!
Upvotes: 2
Reputation: 2973
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
Upvotes: 9
Reputation: 10174
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
Upvotes: 2
Reputation: 1118
Try this:
<%= link_to 'download', root_path << '/assets/docs/Физика.pdf' %>
Upvotes: 1