没有视图的Rails邮件程序

aur*_*els 15 mailer ruby-on-rails-3

是否有可能没有任何视图的Rails 3邮件?

处理纯文本邮件时,能够将正文放在邮件程序本身并且不必仅使用一行文本(或一个I18n键)就可以使用视图进行操作会很酷.

在某种程度上,我正在寻找类似ActionController的"render:text =>",但是对于ActionMailer.

Noé*_*her 22

更简单,只需使用body选项:

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

如果您打算使用html,请不要忘记使用默认为text/plaincontent_type选项指定.

content_type: "text/html"
Run Code Online (Sandbox Code Playgroud)


因此,使用body选项rails会跳过模板渲染步骤.


aur*_*els 17

我通过实验找到了方法:

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

这在Rails指南中也有说明:http: //guides.rubyonrails.org/action_mailer_basics.html第2.4节