I use this feature in my project:
class DownloadController < ApplicationController
before_action :authenticate, only: :billing_pdf
def billing_pdf
# i save my pdf file in private folder
send_file Rails.root.join('private', "billing_pdf"), type: "application/pdf", x_sendfile: true
end
def authenticate
authenticate_or_request_with_http_basic('Administration') do |username, password|
username == 'admin' && password == 'password'
end
end
end
in views:
<%= link_to 'Download Billing', download_billing_pdf_path %>
in routes:
namespace :download do
get 'billing_pdf'
end