ruby ActionView在预览中缺少模板,如何解决?

9lowa7mx  于 2023-01-30  发布在  Ruby
关注(0)|答案(1)|浏览(153)

我正在尝试让ActionMailer发送一封带有Letter_Opener gem的电子邮件。当我尝试使用localhost:3000/mailer/order_mailer查看预览时,终端记录了此错误:

app/mailers/order_mailer.rb:10:in `new_order_email'
Started GET "/rails/mailers/order_mailer/new_order_email" for ::1 at 2023-01-27 16:27:38 -0800
Processing by Rails::MailersController#preview as HTML
  Parameters: {"path"=>"order_mailer/new_order_email"}
OrderMailer#new_order_email: processed outbound mail in 1.2ms
Completed 500 Internal Server Error in 24ms (Allocations: 9119)
  
ActionView::MissingTemplate (Missing template order_mailer/new_order_email with "mailer".

Searched in:
  * "order_mailer"
):

应用程序/邮件程序/订单邮件程序.rb

class OrderMailer < ApplicationMailer
  default from: 'actionMailerTester2301@gmail.com'

  # layout "order_mailer"
  
  def new_order_email
    @email = 'joeDANE@example.com'

    mail(to: @email, subject: "Thank you for your donation.")

  end
end

在浏览器上,mail(to:.....行被指出,我已经查看了多个stackoverflow条目,但我无法理解它,尽管我认为这是因为我对Ruby不熟悉,不打算理解这个问题。
我希望如果我能找出为什么预览不工作,我会找出与开信宝石的次要问题。

2guxujil

2guxujil1#

Ruby on Rails更喜欢遵循某些约定而不是配置。在大多数情况下,遵循Rails的方式要简单得多,而不是过度定制应用程序。
在这个特定的示例中,我建议不要更改或重新配置Rails查找邮件程序视图的位置,而只是将视图放在默认文件夹中。
遵循惯例,对于邮件程序视图以及邮件程序及其方法的命名方式,视图应按如下方式命名并位于app/views文件夹中(而不是app/mailers/views中):

app/views/order_mailer/new_order_email.html.erb
app/views/order_mailer/new_order_email.text.erb

阅读官方Rails指南中关于mailer视图命名的内容。

相关问题