2017-04-17 173 views
4

我想發送一個電子郵件附件使用動作郵件在軌道上的紅寶石,我不斷收到此錯誤。問題似乎是它無法在我指定的目錄中找到文件,但文件路徑是有效的。我還在控制檯中使用File.exist?進行了檢查,並確認提供的路徑評估爲true。Errno :: ENOENT - 沒有這樣的文件或目錄@ rb_sysopen

這裏是我的郵件:

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 

我還安裝了郵件寶石處理的編碼,由Action梅勒文檔的建議。

任何幫助將不勝感激, -Brian

+0

這可以幫助https://stackoverflow.com/questions/6566884/ruby​​s-file-open-given-no-such-file-or-directory-text-txt-errnoenoent-er –

+0

https://stackoverflow.com/questions/15605782/errnoenoent-no-such-file - 或 - 目錄 - 紅寶石 –

回答

6

Rails.root返回Pathname對象。 Pathname#+(string)File.joinstring改爲路徑如果它是相對的;如果string表示絕對路徑(即以斜槓開始),則路徑取代替換爲

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

這意味着,你在錯誤的道路閱讀:你想讀/path/to/app/public/albums/files.zip,但你實際上是閱讀/public/albums/files.zip,這可能不應該存在。

解決方法:請確保您附加的相對路徑:

Rails.root + 'public/albums/files.zip' 
+0

這工作完美!十分感謝你的幫助! – bdowe

+0

@bdowe我認爲你應該用tick標記這個答案(就像我接受的那樣)! –

-1

Rails.root返回路徑對象。所以,你需要它toconvert串用另一個字符串來連接,如下所示: (Rails.root.to_s +「/public/albums/files.zip」)

相關問題