0

This is my controller action to download a file.

def download_file
    doc = Doc.find(params[:doc_id])
    address = doc.file_name.file.path
    puts("ADDRESS: ")
    puts(doc.file_name.file.path)

    send_file(doc.file_name.file.path,
        :filename =>doc.short_name,
        :type => doc.file_name.content_type,
        :disposition => 'attachment',
        :url_based_filename => true)
end

I have no problem with downloading most of the files. However, I received this error message that i do not know how to handle

Parameters: {"doc_id"=>"5"}
Doc Load (0.2ms)  SELECT  "docs".* FROM "docs" WHERE "docs"."id" = ? LIMIT ?  [["id", 5], ["LIMIT", 1]]
ADDRESS: 
/home/ubuntu/workspace/public/uploads/doc/file_name/5/test.gh
Sent file /home/ubuntu/workspace/public/uploads/doc/file_name/5/test.gh (2.0ms)
Completed 500 Internal Server Error in 5ms (ActiveRecord: 0.2ms)
NoMethodError (undefined method `empty?' for nil:NilClass):

Thanks!

Answer

I figured out a solution and I am sharing it here for others who may face the same challenge. Removing the additional arguments from the 'send_file' function solves the problem. It seems when the file type is unknown an exception is likely to be raised. The following code worked for me just fine.

def download_file
    doc = Doc.find(params[:doc_id])
    send_file(doc.file_name.file.path)
end

0 Answers0