【问题标题】:Mailer error missing template邮件程序错误缺少模板
【发布时间】:2015-09-08 04:00:48
【问题描述】:

你好,

当我尝试执行操作时,ActionMailer 出现问题:

rake send_email

我收到一个错误:

    rake aborted!
ActionView::MissingTemplate: Missing template user_mailer/mailer with "mailer". Searched in:
  * "user_mailer"

这是我的:

ma​​ilers/user_mailer.rb

class UserMailer < ActionMailer::Base
  default from: "from@example.com"

  def mailer(user)
    @user = user
    mail(to: @user.email, subject: 'Test')
  end

end

views/user_mailer/mailer.html.erb

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <p>
      Sample mail.
    </p>
  </body>
</html>

views/user_mailer/mailer.text.erb

Sample mail.

lib/tasks/emails_task.rake

desc 'send email'
task send_email: :environment do
  UserMailer.mailer(User.last).deliver!
end

config/environments/development.rb

# I recommend using this line to show error
  config.action_mailer.raise_delivery_errors = true

  # ActionMailer Config
  config.action_mailer.delivery_method = :letter_opener

# config.action_mailer.default_url_options = { :host => 'localhost:3000' }
# config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
 :address              => "smtp.gmail.com",
 :port                 => 587,
 :user_name            => ENV['gmail_username'],
 :password             => ENV['gmail_password'],
 :authentication       => "plain",
:enable_starttls_auto => true
}

# Send email in development mode?
config.action_mailer.perform_deliveries = true

我在 stackoverflow 上搜索了解决方案,并尝试了许多类似问题的答案,但不幸的是,它们都不适合我。

我找到了解决方案,当我将正文添加到邮件方法时:

def mailer(user)
  @user = user
  mail(to: @user.email, subject: 'Test', body: 'something')
end

然后它确实可以工作,但我希望在单独的文件中有一个正文,并使其与用户名和其他内容更加复杂。

如果有人知道如何解决这个问题,我将非常感激:)

【问题讨论】:

  • 我发现了同样的问题,并且您的修复工作有效(尽管我现在偶尔仍会遇到故障。)有趣的是,这只发生在我的生产环境中。开发工作得很好。我仍在试图弄清楚发生了什么。
  • 我也有同样的问题!刚开始赏金:-)
  • 你使用什么时候运行这个?尝试像这样运行命令RAILS_ENV=production rake send_email
  • 你检查过这个railscasts.com/episodes/206-action-mailer-in-rails-3 就像你想要的我猜
  • 我找到了解决这个问题的办法。我在其中一个文件夹中没有英文字符,因此我遇到了这个问题。当我将文件夹名称更改为其他名称时,问题已解决。

标签: ruby-on-rails ruby ruby-on-rails-4 rake actionmailer


【解决方案1】:

您能否发布一个极简的 Github 存储库来重现您的错误?

您可以尝试生成您的邮件,以检查您没有忘记某些东西:

bundle exec rails generate mailer mailer_classname mailer_viewname

在你的情况下:

bundle exec rails generate mailer user_mailer mailer

【讨论】:

    【解决方案2】:

    尝试添加布局

    class UserMailer < ActionMailer::Base
      default from: "from@example.com"
    
      layout "mailer"
    
      def mailer(user)
       @user = user
       mail(to: @user.email, subject: 'Test')
     end
    
    end
    

    【讨论】:

      【解决方案3】:

      Rails 很可能只是找不到您新创建的文件。如果您正在运行spring gem,那可能就是问题所在。 (更多关于春天的信息:https://github.com/rails/spring

      设置终端命令并停止运行spring

       $ bundle exec spring binstub --all
       $ bin/spring stop
      

      我试过这个,然后用rails s 重新运行我的应用程序。那没有用,因此请尝试完全关闭终端并重新启动计算机。运行 rails s 并尝试在第二个选项卡中使用 rails c 进行测试:

       mail = UserMailer.mailer(User.last)
      

      终于为我工作了,希望它能帮助你们中的一些人!

      (其中一些步骤可能是多余的。)

      【讨论】:

        【解决方案4】:

        我重命名了我的方法并且它有效。可能方法名不能是mail或者mailer...

        【讨论】:

        • 你能具体点吗?究竟是什么方法?
        • @ChristianFazzini def mailer(user)
        【解决方案5】:
        class UserMailer < ActionMailer::Base
          default from: "from@example.com"
        
          layout "mailer"
        
          def mailer(user)
           @user = user
           mail(to: @user.email, subject: 'Test')
         end
        
        end
        

        在layout下添加html布局#

        mailer.html.erb

        <!DOCTYPE html>
        <html>
          <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <style>
              /* Email styles need to be inline */
            </style>
          </head>
        
          <body>
            <%= yield %>
          </body>
        </html>
        

        在布局下添加文字布局#

        mailer.text.erb

        <%= yield %>
        

        使用预览根据您的测试框架检查您的电子邮件。

        【讨论】:

          【解决方案6】:

          我遇到了同样的问题,我的解决方案是删除邮件模板文件,在您的情况下为 views/user_mailer/mailer.html.erb,然后重新创建它。 看来我创建的文件具有正确的名称,但在某处有一些奇怪的空白,而 ActionMailer 没有识别它。

          【讨论】:

            【解决方案7】:

            我遇到了同样的问题。重新启动 sidekiq 为我解决了这个问题。

            【讨论】:

              【解决方案8】:

              我有一个类似的问题。我错过了在我的 Gemfile 中添加 gem 'haml-rails'。检查您是否缺少宝石。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2015-09-08
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多