Action Mailer附件的未定义方法URL

时间:2014-06-25 05:19:18

标签: ruby-on-rails ruby-on-rails-4 actionmailer

我得到的错误:

ActionView::Template::Error (undefined method `url' for nil:NilClass)

我有这个附件正在增加

<%= image_tag attachments['tippedlogods.png'].url %>

话虽如此,我已在方法中声明附件以发送电子邮件,此处:

  def confirmation(order)
    @order = order
    mail(to: @order.email, subject: "Order Confirmation", content_type: "text/html")
    attachments.inline['tippedlogods.png'] = File.read("app/assets/images/tippedlogods.png")
  end

在我的produ.rb中:

  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.default_url_options = { :host => 'hidden-peak-xxxx.heroku.com', :only_path => false}
  config.action_mailer.asset_host = 'hidden-peak-xxxx.heroku.com'
  ActionMailer::Base.smtp_settings = {
  :address    => "smtp.sendgrid.net",
  :port       => 587,
  :user_name  => ENV['SENDGRID_USERNAME'],
  :password   => ENV['SENDGRID_PASSWORD'],
  :domain     => ENV['SENDGRID_DOMAIN'],
  :authentication  => :plain
  }

这不是在确认电子邮件中发送图像的方法吗?

我正在关注RoR指南here ...

注意 Rails 4~

1 个答案:

答案 0 :(得分:6)

在添加附件之前,您先调用mail。所以在呈现模板时没有附件!

http://apidock.com/rails/ActionMailer/Base/mail

更改陈述的顺序。

attachments.inline['tippedlogods.png'] = File.read("app/assets/images/tippedlogods.png")
mail(to: @order.email, subject: "Order Confirmation", content_type: "text/html")