Search code examples
ruby-on-railsrubyruby-on-rails-5actionmailerirb

undefined method `default' for ActionMailer


I'm having this weird error with Rails 5 action mailer.

# app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
  default from: "from@example.com"
  layout 'mailer'
end

irb

~/workspace/rails5 > rails c       ruby-2.4.1@rails5
Running via Spring preloader in process 45498
Loading development environment (Rails 5.1.2)
2.4.1 :001 > ApplicationMailer
     NameError: uninitialized constant Mail::TestMailer
     from app/mailers/application_mailer.rb:1:in `<top (required)>'
     from (irb):1
2.4.1 :002 > ApplicationMailer
     NoMethodError: undefined method `default' for ApplicationMailer:Class
     from app/mailers/application_mailer.rb:3:in `<class:ApplicationMailer>'
     from app/mailers/application_mailer.rb:1:in `<top (required)>'
     from (irb):2
2.4.1 :003 >

This is the default Scaffold of rails 5.

Two really weird things happen the first is that it always tells me the uninitialized constant then trows default is not a method right after.

Just wondering how to fix it or if I should downgrade to Rail 5.0


Solution

  • The solution turned out to be that if you have a model named mail the ActionMailer will grab that one first instead of the rails class. If this happens then it fails the first time but the second time will load the rails class and try and smash them together. Allowing the submodule to work but treating the main class as a monkey path.