Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix my UserMailer / Devise::Mailer to satisfy deprecation warning in Rails 6?

I customize the Devise default mailers by inheriting them in UserMailer but this had triggered a deprecation warning. I don't really understand what is it telling me.

DEPRECATION WARNING: Initialization autoloaded the constants Devise::Mailer, ApplicationHelper, and UserMailer.

Being able to do this is deprecated. Autoloading during initialization is going
to be an error condition in future versions of Rails.

Reloading does not reboot the application, and therefore code executed during
initialization does not run again. So, if you reload Devise::Mailer, for example,
the expected changes won't be reflected in that stale Class object.

These autoloaded constants have been unloaded.
# devise.rb

...
config.mailer = UserMailer
config.parent_mailer = 'ApplicationMailer'
...
# user_mailer.rb

class UserMailer < Devise::Mailer
  helper :application
  include Devise::Controllers::UrlHelpers
  ...
end
like image 696
Mysterywood Avatar asked Oct 20 '25 03:10

Mysterywood


1 Answers

To prevent that DEPRECATION warning, you could require those 3 classes (Devise::Mailer, ApplicationHelper, and UserMailer) in your config/application.rb

I ran into a very similar error message: DEPRECATION WARNING: Initialization autoloaded the constants ApplicationRecord, Status, and Task.

I fixed it like this:

# config/application.rb
. . .
module MyApp
  class Application < Rails::Application
    require "#{Rails.root}/app/models/application_record"
    require "#{Rails.root}/app/models/concerns/status"
    require "#{Rails.root}/app/models/task"
. . .

The reason you're seeing that DEPRECATION warning is because Rails 6 defaults to using Zeitwerk to load dependencies and the classes Rails is complaining about, unless fixed, will very likely have problems loading properly in future versions of Rails.

For more info see: https://guides.rubyonrails.org/autoloading_and_reloading_constants.html

like image 140
l3x Avatar answered Oct 21 '25 16:10

l3x



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!