Rails 5 Devise Mailer inline attachments undefined method `url` for nil:NilClass

Viewed 684

I am receiving the error

undefined method `url' for nil:NilClass 

when I am previewing my Devise mailers only. I am trying to add an inline image within my email.

# app/mailers/user_mailer.rb
class UserMailer < Devise::Mailer
   include Devise::Controllers::UrlHelpers
   helper :application
   before_action :add_inline_attachment!

   def reset_password_instructions(record, token, opts={})
     super
   end

   #...

   private
   def add_inline_attachment!
     attachments.inline['logo.jpg'] = File.read('app/assets/images/logo.jpg')
   end
end

In my mailer view:

# app/views/user/mailer/reset_password_instructions.html.erb
<%= image_tag attachments['logo.jpg'].url %>

While setting up Devise I added the following configurations in order to have scoped views

# config/initializers/devise.rb
config.mailer = 'UserMailer'
config.scoped_views = true

The major part of my hair pulling is that this works with every mailer views including devise_invitable. It's the core Devise views password_change, reset_password_instructions, and email_change.

Things I have tried and failed:

# app/views/user/mailer/reset_password_instructions.html.erb
<%= image_tag attachments['logo.jpg'].try(:url) %>

That returns nil is not a valid asset source

I've also tried adding the attachment directly inside:

# app/mailers/user_mailer.rb
class UserMailer < Devise::Mailer
   include Devise::Controllers::UrlHelpers
   helper :application

   #...

   def reset_password_instructions(record, token, opts={})
     attachments.inline['logo.jpg'] = File.read('app/assets/images/logo.jpg')
     super
   end

   #...

end

Also, adding the root for the image:

# Both
attachments.inline['logo.jpg'] = File.read("#{Rails.root}/app/assets/images/logo.jpg")

# And
attachments.inline['logo.jpg'] = File.read(Rails.root.join("app/assets/images/logo.jpg"))
0 Answers
Related