Karlis
Karlis

Reputation: 1501

Ruby on rails file download link

I created file upload using paperclip! File uploads as it is supposed to. Than I added file download method like this:

def download
    sample = Sample.find(params[:id])


    send_file  upload.sample.path,
    :filename => upload.sample_file_name,
                :type => upload.sample_content_type,
    :disposition => 'attachment'
    flash[:notice] = "Your file has been downloaded"
  end

But I cant figure out, what should I put in my show action so that I would be able to download the file?

I got as far as this:

<td><%= @sample.upload_file_name =%></td>
    <%= link_to 'Download', :action => :download, :path =>@sample.upload.url, :type => @sample.upload_content_type %>

But it shows error : Couldn't find Sample with id=download

Can anyone help me?

Upvotes: 2

Views: 7478

Answers (4)

thesis
thesis

Reputation: 2575

It looks like you have mismatch in your routes.

That is why, you have to:

  1. Add additional route to your routes.rb for download action.
  2. As it was said previously, you have to add link to your view (link_to).

Read this post for more advanced Download functionality: http://thewebfellas.com/blog/2009/8/29/protecting-your-paperclip-downloads

Upvotes: 0

Viktor Tr&#243;n
Viktor Tr&#243;n

Reputation: 8894

Assuming you set download named route properly, you can just say

<%= link_to 'Download', download_sample_path(@sample) %>

Upvotes: 0

Yuri  Barbashov
Yuri Barbashov

Reputation: 5437

=link_to 'download', @sample.upload.path This is the easiest way))

Upvotes: 0

Pasted
Pasted

Reputation: 864

Think you need to change your link_to to send the :id (that's what the controller action is looking for with params[:id])

<%= link_to 'Download', :action => :download, :id => @sample.id %>

Soz Karlis was writing as you posted :D

Upvotes: 2

Related Questions