Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errno::ENOENT - No such file or directory @ rb_sysopen

I'm trying to send an email attachment using action mailer in ruby on rails and I keep getting this error. The problem seems to be that it can't locate the file in the directory I specified, but the file path is valid. I also checked this using File.exist? in the console and confirmed that the path provided evaluates to true.

Here is my mailer:

class OrderMailer < ApplicationMailer   
  def purchase(order)
    @order = order
    attachments[ 'files.zip'] = File.read(Rails.root + '/public/albums/files.zip')
    mail to: order.email, subject: "Order Confirmation"
  end
end

I also installed the mail gem to handle encoding, as advised by the Action Mailer documentation.

Any help would be much appreciated, -Brian

like image 742
bdowe Avatar asked Apr 17 '17 06:04

bdowe


1 Answers

Rails.root returns a Pathname object. Pathname#+(string) will File.join the string to the path if it is relative; if string represents an absolute path (i.e. starts with a slash), the path gets replaced.

Pathname.new('/tmp') + 'foo'
# => #<Pathname:/tmp/foo> 
Pathname.new('/tmp') + '/foo'
# => #<Pathname:/foo> 

This means, you are reading from the wrong path: you wanted to read /path/to/app/public/albums/files.zip, but you are actually reading /public/albums/files.zip, which likely shouldn't exist.

Solution: make sure you are appending the relative path:

Rails.root + 'public/albums/files.zip'
like image 134
Amadan Avatar answered Nov 12 '22 20:11

Amadan