簡體   English   中英

子文件夾中的Rails郵件程序和郵件程序視圖無法正常工作

[英]Rails mailer and mailer views in subfolder not working

我有一個郵件,我可以在我的日志中看到發送,但郵件正文不包含郵件程序視圖中的任何內容。

這是因為我把東西放在子文件夾中,我嘗試在我的mail功能中使用:template_path ,但無濟於事。

應用程序/郵寄/營銷/ marketing_mailer.rb

class Marketing::MarketingMailer < ActionMailer::Base

    require 'mail'
    address = Mail::Address.new "test@example.com" # ex: "john@example.com"
    address.display_name = "Text" # ex: "John Doe"
    # Set the From or Reply-To header to the following:
    address.format # returns "John Doe <john@example.com>"
    default from: address

    # Sends an email when someone fills out the contact form
    def contact(name, email, message)
        @name = name
        @email = email
        @message = message
        mail(:subject => "Test", :to => 'test@example.com', :reply_to => @email) # <== I've tried using :template_path => 'marketing', 'marketing/marketing_mailer', etc, but none worked.
    end
end

/app/views/marketing/marketing_mailer/contact.html.erb

<!DOCTYPE html>
<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
  </head>
  <body>
    <p>Name: <%= @name %></p>
    <p>Email: <%= @email %></p>
    <p>Message: <%= @message %></p>
  </body>
</html>

我注意到devise在/ views / devise / mailers /中有郵件視圖...所以我知道這是可能的,但我不確定如何。

您是否嘗試使用{:template_path =>'marketing / marketing_mailer,:template_name =>'contact'}郵件?

模板名稱可能以某種方式期望文件名中的命名空間

沒有模板渲染它是否有效

   class Marketing::MarketingMailer < ActionMailer::Base
   def welcome_email(user, email_body)
     mail(to: user.email,
     body: email_body,
     content_type: "text/html",
     subject: "Already rendered!")
   end
   end

由於您的文件格式為.html.erb ,因此您可能必須指定要使用此格式。 像這樣:

mail(:subject => "Test", :to => 'test@example.com', :reply_to => @email) do
  format.html
end

通常,您的模板應該自動拾取,因為名稱匹配,但可能有一個同名的第二個文件以.text.erb結尾或其他一些妨礙模板查找的文件。

EDIT1 (瘋狂猜測)除了模板查找問題,我能想象的唯一其他問題來源是你的實例變量。 也許@message應該是別的東西,你干擾內部,所以重命名它可能會有所幫助。 根據apidock上的代碼,郵件在某些地方使用@variables 我承認,這很遙遠。

EDIT2 (這里面臨的問題的解釋)實際問題背后的行為 - 在服務器重啟之前找不到新模板 - 的工作原理如下:

查找模板,但那時,它還沒有出現。 服務器找不到它,但記得它不存在的事實。 然后在下次重啟之前不會再查找它,即使您已添加它。

所以,重啟將解決這個問題。

你試過把它關掉再打開嗎?

我今天回到這個項目,似乎今天找到模板沒有必要指定template_path沒問題。

盡管很遺憾,但顯然我需要做的就是停止並重新啟動我的服務器,以便我的郵件程序可以獲取。 由於其他視圖在不停止和啟動我的服務器的情況下被拾取,我對此感到非常驚訝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM