Ruby on rails 未定义的方法filename=用于操作邮件程序附件
当试图执行Ruby on rails 未定义的方法filename=用于操作邮件程序附件,ruby-on-rails,ruby,actionmailer,Ruby On Rails,Ruby,Actionmailer,当试图执行 attachment report[1] do |a| a.body = File.read(report[0]) a.filename = report[0].gsub(/.*\//,'') end 我得到以下错误 undefined method `filename=' for #<Mail::Part:0x929b3b0> 创建附件只创建test.txt文件。在这种情况下,邮件与附件一起发送,但发送的txt文件为空。您尝试过吗 a
attachment report[1] do |a|
a.body = File.read(report[0])
a.filename = report[0].gsub(/.*\//,'')
end
我得到以下错误
undefined method `filename=' for #<Mail::Part:0x929b3b0>
创建附件只创建test.txt文件。在这种情况下,邮件与附件一起发送,但发送的txt文件为空。您尝试过吗
attachments[report[0].gsub(/.*\//,'')] = File.read(report[0])
您是否尝试过:
attachments[report[0].gsub(/.*\//,'')] = {
:mime_type => 'text/plain',
:content => File.read(report[0]),
:content_disposition => 'attachment'
}
这可能会有帮助。
您的test.txt文件应位于公用文件夹中
attachment "text/plain" do |a|
a.filename = "test.txt"
a.body = File.read(RAILS_ROOT + "/public/test.txt")
end
和excel文件
attachment "application/vnd-ms-excel" do |a|
a.filename = "test.xls"
a.body = File.read(RAILS_ROOT + "/public/test.xls")
end
试试这个
mail.attachments[report[0].split('/').last] = File.read(report[0])
现在,当设置:body和:content时,附件显示在邮件正文中,而不是作为可下载的附件。它适用于文本文件,但不适用于excel文件附件:content\u type=>“text/plain”,:filename=>“一些无用的附件”,:body=>file.read(@file\u name),:content=>file.read(@file\u name)您使用的是ActionMailer的哪个版本,2还是3?
mail.attachments[report[0].split('/').last] = File.read(report[0])