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
felipeclopes Oct 31 '12 at 18:13 2012-10-31 18:13
source share