Cannot read file with send_file function in Rails 3

Mohit Jain picture Mohit Jain · Dec 5, 2011 · Viewed 7.4k times · Source

The code I am using:

In view file:

 <%= link_to "Download", download_image_path(image) %>

In controller:

def download
  @image = Image.find(params[:id])
  send_file "#{RAILS_ROOT}/public"  + @image.attachment.url
end

I am getting an error:

Cannot read file /Users/mohit/projects/my_app/public/system/attachments/4/original/Screen Shot 2011-11-04 at 3.14.03 PM.png?1320582022

PS: double checked, the file exists. Same issue on server, for all the files (images, pdfs, videos) in all the respective controllers.

Answer

Mohit Jain picture Mohit Jain · Dec 6, 2011

Issue was:

I was using

 @image = Image.find(params[:id])
 send_file "#{RAILS_ROOT}/public"  + @image.attachment.url

It should be

 @image = Image.find(params[:id])
 send_file  @image.attachment.path

PS: Make sure you validate that image/record exists.