ruby 带导轨的ActionMailer:未初始化的常量UserMailer

cig3rfwq  于 2022-12-18  发布在  Ruby
关注(0)|答案(1)|浏览(192)

尝试UserMailer.welcome.deliver_now时在控制台中出现此错误

(irb):6:in `<main>': uninitialized constant UserMailer (NameError)

UserMailer.welcome.deliver_now
^^^^^^^^^^
Did you mean?  UserMailerPreview

在我得到这个NameError之前,控制台抛出了方法welcome未定义的错误。
enter image description here
这是我的文件结构。
app/mailers/application_mailer.rb

class ApplicationMailer < ActionMailer::Base
  default from: "from@example.com"
  layout "mailer"
end

app/mailers/user_mailer.rb

class UserMailer < ApplicationMailer  
  def welcome
    @greeting = "Hi"
    mail to: "to@example.org"
  end
end


app/user_mailer/welcome_email.html.erb

<h1><%= @greeting %>,</h1>
<p>Example Email Body</p>

app/user_mailer/welcome_email.text.erb

<%= @greeting %>,

Example Email Body
pgky5nke

pgky5nke1#

你说:
app/mailers/application_mailer.rb

class ApplicationMailer < ActionMailer::Base
  default from: "from@example.com"
  layout "mailer"
end

app/mailers/user_mailer.rb

class UserMailer < ApplicationMailer  
  def welcome
    @greeting = "Hi"
    mail to: "to@example.org"
  end
end

但事实并非如此。根据你的截图,这些文件在app/views/mailers中。Rails无法正确自动加载它们。请将它们移动到正确的位置,然后重试。

相关问题