(データベース内の) メール モデルの body 属性を使用してメールをレンダリングするための Action Mailer セットアップがあります。本文で erb を使用できるようにしたいのですが、送信された電子メール メッセージでレンダリングする方法がわかりません。
このコードで本体を文字列として取得できます
# models/user_mailer.rb
def custom_email(user, email_id)
email = Email.find(email_id)
recipients user.email
from "Mail It Example <admin@foo.com>"
subject "Hello From Mail It"
sent_on Time.now
# pulls the email body and passes a string to the template views/user_mailer/customer_email.text.html.erb
body :msg => email.body
end
この記事http://rails-nutshell.labs.oreilly.com/ch05.htmlに出くわしましたが、使用できると書かれていますが、作業render
することしかできずrender :text
、作業することはできませんrender :inline
# models/user_mailer.rb
def custom_email(user, email_id)
email = Email.find(email_id)
recipients user.email
from "Mail It Example <admin@foo.com>"
subject "Hello From Mail It"
sent_on Time.now
# body :msg => email.body
body :msg => (render :text => "Thanks for your order") # renders text and passes as a variable to the template
# body :msg => (render :inline => "We shipped <%= Time.now %>") # throws a NoMethodError
end
更新:initialize_template_class
誰かがこのスレッドhttp://www.ruby-forum.com/topic/67820での使用を推奨しました。私は今これを持っていますbody
body :msg => initialize_template_class(:user => user).render(:inline => email.body)
それは機能しますが、私はこれを理解していないので、プライベートメソッドを調査してみましたが、これはハックであり、おそらくもっと良い方法があるのではないかと心配しています。 提案?