0

我将 ruby​​ 从 1.8.x 升级到 1.9.3

Pony.mail(
    :to => to, 
    :from => from,
    :subject => subject, 
    :body => Nokogiri::HTML(body_with_footer).text, 
    :html_body =>  body_with_footer, #.gsub("\n","<BR>"),
    :attachments => attachment_to_send,
    :via => :smtp, 
    :via_options => {
            :address     => $smtp,
            :port     => $smtp_port,
            :enable_starttls_auto => false
    }
)

attachment_to_send 应该是要附加的文件的哈希值。当哈希为空时,不发送附件。现在我收到一个小马错误,抱怨哈希是“”​​。

所以我引入了一个 if 条件attachment_to_send=="",所以我称 pony 有或没有附件部分。

有什么办法可以管理吗?所以我只有一个叫小马的代码?

4

2 回答 2

1

通过以下方式检查空条件来准备您的附件数组,

 tmp_hash = {:to => to, 
             :from => from,
             :subject => subject, 
             :body => Nokogiri::HTML(body_with_footer).text, 
             :html_body =>  body_with_footer, #.gsub("\n","<BR>"),
             :via => :smtp, 
             :via_options => {
                            :address     => $smtp,
                            :port     => $smtp_port,
                            :enable_starttls_auto => false
                             }
             } 

tmp_hash[:attachments] => attachment_to_send
tmp_hash[:attachments] => nil if attachment_to_send.empty?

或直接,

 tmp_hash[:attachments] =>  attachment_to_send if not attachment_to_send.empty?

进而

Pony.mail( tmp_hash)

应该管用

于 2012-09-12T06:10:44.113 回答
1

用三元运算符处理attachment_to_send.empty? ? nil : attachment_to_send

      details = {
            :to => to, 
            :from => from,
            :subject => subject, 
            :body => Nokogiri::HTML(body_with_footer).text, 
            :html_body =>  body_with_footer, #.gsub("\n","<BR>"),
            :attachments => attachment_to_send.empty? ? nil : attachment_to_send ,
            :via => :smtp, 
            :via_options => {
                    :address     => $smtp,
                    :port     => $smtp_port,
                    :enable_starttls_auto => false
            }


Pony.mail(details)
于 2012-09-12T06:20:03.923 回答