Errno :: ENOENT - There is no such file or directory @rb_sysopen

I am trying to send an email attachment using the ruby ​​action mail program on rails and I keep getting this error. The problem is that it cannot find the file in the directory you specified, but the file path is valid. I also checked this with File.exist? in the console and confirmed that the specified path is evaluated as true.

Here is my mail:

 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 set up a gemstone to handle encoding, as described in the Action Mailer documentation.

Any help would be greatly appreciated, Brian

+5
source share
2 answers

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

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

This means that you are reading the wrong way: you wanted to read /path/to/app/public/albums/files.zip , but in fact you are reading /public/albums/files.zip , which probably should not exist.

Solution: make sure you add the relative path:

 Rails.root + 'public/albums/files.zip' 
+7
source

Rails.root returns a path object. Therefore, you need to convert it to a string in order to associate it with another string as follows: ( Rails.root.to_s + '/public/albums/files.zip')

-1
source

Source: https://habr.com/ru/post/1266807/


All Articles