簡體   English   中英

沒有視圖的 Rails 郵件

[英]Rails mailer without view

是否可以在沒有任何視圖的情況下使用 Rails 郵件程序?

在處理純文本郵件時,能夠將正文放在郵件程序本身中並且不必使用僅包含一行文本(或一個 I18n 鍵)的操作視圖會很酷。

在某種程度上,我正在尋找類似 ActionController 的“render :text =>”的東西,但是對於 ActionMailer。

更簡單,只需使用body選項:

def welcome(user)
  mail to:       user.email,
       from:     "\"John\" <admin@domain.ch>",
       subject: 'Welcome in my site',
       body:    'Welcome, ...'
end

如果您打算使用 html,請不要忘記使用content_type選項指定它,默認情況下是text/plain

content_type: "text/html"


因此,使用body選項 rails 會跳過模板渲染步驟。

我通過實驗找到了方法:

mail(:to => email, :subject => 'we found the answer') do |format|
  format.text do
    render :text => '42 owns the World'
  end
end

這也在 Rails 指南中說: http : //guides.rubyonrails.org/action_mailer_basics.html在第 2.4 節

暫無
暫無

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

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