Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Rails 4:</strong></p> <p>in routes:</p> <pre><code>get "home/download_pdf" </code></pre> <p>in controller (already have pdf):</p> <pre><code>def download_pdf send_file( "#{Rails.root}/public/your_file.pdf", filename: "your_custom_file_name.pdf", type: "application/pdf" ) end </code></pre> <p>in controller (need to generate pdf):</p> <pre><code>require "prawn" class ClientsController &lt; 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 </code></pre> <p>in view:</p> <pre><code>&lt;%= link_to 'Download PDF', home_download_pdf_url %&gt; </code></pre> <p><strong>Rails 3</strong></p> <p>The way to do it:</p> <pre><code>def download send_data pdf, :filename =&gt; "abc.pdf", :type =&gt; "application/pdf" end </code></pre> <p>You should go to this alternative</p> <p><strong>Rails &lt; 3</strong></p> <p><strong>File in public folder</strong></p> <p>This may the the answer to you: <a href="https://stackoverflow.com/questions/6392003/how-to-download-a-file-from-rails-application">How to download a file from rails application</a></p> <p>You should place your file in public folder, that is the trick.</p> <p>Should work when the file is placed correctly.</p> <p>Let me know if you can't move your file to public folder.</p> <p><strong>Download via controller</strong></p> <p>Create a controller with a downlaod action and <code>link_to</code> it</p> <pre><code> def download send_file '/assets/data/abc.pdf', :type=&gt;"application/pdf", :x_sendfile=&gt;true end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload