我有一些使用Whenever gem每周运行的代码。它会创建一个文件并发送下载链接。
ApplicationMailer
def weekly_email
file = CSVData::Report.new(time).create_csv
@filename = File.basename(file.path)
mail(
:from => "from",
:to => "to",
:subject => "Data #{time.strftime("%m/%d/%Y")}"
)
end
该文件存储在downloads/
中。问题是,它在测试中起作用,它有时会起作用,但有时它会生成一个丢失文件错误:
错误:
ActionController::MissingFile occurred in file_downloads#download:
Cannot read file downloads/data_9_17_2015.csv
actionpack (3.2.13) lib/action_controller/metal/data_streaming.rb:71:in `send_file'
以下是下载代码:
def download
send_file "downloads/#{params[:filename]}.csv", type: "application/csv", x_sendfile: true
end
以下是文件创建的缩短版本:
def create_file
file_data = some_cool_data
file = create_file(File.join(Dir.pwd, "/downloads/#{@file_name}.csv"))
file.write(file_data)
file.close
file
end
def create_file(path)
FileUtils.mkdir_p(File.dirname(path))
File.new(path, "w+")
end
我不认为它是文件引用的问题(即/dowloads
vs downloads
),因为它在测试中有效,有时在生产中。它似乎被删除了。什么可能导致丢失的文件错误?