Undefined Method URL for Mailer Attachment Application

The error I am getting is:

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

I have this application that sends nil

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

With this, I declared the attachment in the method for sending email here:

  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

In my production.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
  }

Isn't this a way to send an image in a confirmation email?

I followed RoR Guides here ...

Rails Note 4 ~

+4
source share
1 answer

You call mailfirst before adding the attachment. So there is no attachment when it displays a template!

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

Reorder the instructions.

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

Source: https://habr.com/ru/post/1545937/


All Articles