undefined method filename =用于动作邮件程序附件

时间:2011-07-05 13:10:09

标签: 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>

report [1] = application / vnd.ms-excel report [0] =我文件的路径

尝试使用简单文本文件的另一种方法也失败

@file_name = 'test.txt'
    create_attachment
    attachment :content_type => "text/plain",
      :filename => "Some useless attachment",
      :body => File.read(@file_name)

create_attachment只创建test.txt文件。在这种情况下,邮件与附件一起发送,但发送的txt文件为空。

4 个答案:

答案 0 :(得分:2)

你试过这个吗?

attachments[report[0].gsub(/.*\//,'')] = File.read(report[0])

答案 1 :(得分:0)

你试过了吗?

attachments[report[0].gsub(/.*\//,'')] = { 
  :mime_type => 'text/plain', 
  :content => File.read(report[0]),
  :content_disposition => 'attachment'
 }

答案 2 :(得分:0)

这可能会有所帮助。 并且您的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

答案 3 :(得分:0)

试试这个

mail.attachments[report[0].split('/').last] = File.read(report[0])