Link to download the file in rails

I would like to give visitors the opportunity to download some kind of pdf file. I tried:

<%= link_to "abc", "/data/abc.pdf"%> <%= link_to "abc", "/data/abc.pdf", :format => 'pdf' %> 

and some options, but they don't seem to work. I keep getting No route matches [GET] "/data/abc.pdf"

I have PDF files in a folder called data located in the resources folder. Any help would be appreciated.

+48
ruby-on-rails ruby-on-rails-3 hyperlink download link-to
Oct 31 '12 at 18:04
source share
4 answers

Rails 4:

in routes:

 get "home/download_pdf" 

in the controller (already have pdf):

 def download_pdf send_file( "#{Rails.root}/public/your_file.pdf", filename: "your_custom_file_name.pdf", type: "application/pdf" ) end 

in the controller (you need to create a pdf):

 require "prawn" class ClientsController < ApplicationController def download_pdf client = Client.find(params[:id]) send_data generate_pdf(client), filename: "#{client.name}.pdf", type: "application/pdf" end private def generate_pdf(client) Prawn::Document.new do text client.name, align: :center text "Address: #{client.address}" text "Email: #{client.email}" end.render end end 

in sight:

 <%= link_to 'Download PDF', home_download_pdf_url %> 

Rails 3

The way to do this is:

 def download send_data pdf, :filename => "abc.pdf", :type => "application/pdf" end 

You should go to this alternative

Rails <3

File in shared folder

This may give you the answer: How to download a file from a rails application

You must put your file in a shared folder, i.e. trick.

Should work when the file is installed correctly.

Let me know if you cannot move the file to the shared folder.

Download via controller

Create a controller with downlaod and link_to it action

  def download send_file '/assets/data/abc.pdf', :type=>"application/pdf", :x_sendfile=>true end 
+47
Oct 31 '12 at 18:13
source share

Rails 4:

in routes:

 get "home/download_pdf" 

in the controller (already have pdf):

 def download_pdf send_file( "#{Rails.root}/public/your_file.pdf", filename: "your_custom_file_name.pdf", type: "application/pdf" ) end 

in the controller (you need to create a pdf):

 require "prawn" class ClientsController < ApplicationController def download_pdf client = Client.find(params[:id]) send_data generate_pdf(client), filename: "#{client.name}.pdf", type: "application/pdf" end private def generate_pdf(client) Prawn::Document.new do text client.name, align: :center text "Address: #{client.address}" text "Email: #{client.email}" end.render end end 

in sight:

 <%= link_to 'Download PDF', home_download_pdf_url %> 
+71
Dec 25 '13 at 9:10
source share

If the files are static (this means they are not modified), put them in a shared folder.

Then you can download as

 <a href="file.pdf" download>PDF</a> 

or with ERB

 <%= link_to 'PDF', 'file.pdf', download: '' %> 

and provide the file with a different name for download, just pass that name to the download option

 <%= link_to 'PDF', 'file.pdf', download: 'data' %> 

This will load the file as data.pdf instead of file.pdf .

+10
Jul 11 '16 at 19:54
source share

you can just call your controller action like this

 <%= link_to "Download", download_file_path, class: "btn btn-sm btn-default", target: "_blank" %> 

and in your controller

 def download_file redirect_to paperclip_attachment.file.url end 
+3
Feb 15 '16 at 14:19
source share



All Articles