2016年7月5日 星期二

在Mailer中使用Helper Method

有的時候Mailer的內容會跟View差不多,但是View裡面還會有partial跟helper method,partial還好解決,只要把路徑加上去就可以了,問題在於helper method會抓不到,會顯示ActionView::Template::Error: undefined method `helper method name' 的錯誤訊息。



解決方法

可以在mailer\application_mailer.rb中把Helper加進來,如下:
class ApplicationMailer < ActionMailer::Base
  add_template_helper(RealLivesHelper)  #加入這一行,並填上要使用的Helper
  default from: "service@moon-project.com"
  layout 'mailer'
end
這樣做只要有繼承ApplicationMailer的Mailer class就都可以使用到指定Helper中的方法了。

沒有留言: