How to use my view helpers in my ActionMailer views?

后端 未结 7 1751
清酒与你
清酒与你 2020-11-30 18:17

I want to use the methods I defined in app/helpers/annotations_helper.rb in my ReportMailer views (app/views/report_mailer/usage_report.text.html.erb

7条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 18:52

    For Ruby on Rails 4, I had to do 2 things:

    (1) As Duke already said, if the helper you want to add is UsersHelper for example, then add

    helper :users
    

    to the derived ActionMailer class (e.g. app/mailers/user_mailer.rb)

    (2) After that, I got a new error:

    ActionView::Template::Error (Missing host to link to! Please provide the :host
    parameter, set default_url_options[:host], or set :only_path to true)
    

    To fix this, add the line

    config.action_mailer.default_url_options = { :host => 'localhost' }
    

    to each of the config/environments/*.rb files. For config/environments/production.rb, replace localhost with a more appropriate host for the production helper-generated urls.


    Q: For #2, why does the mail view need this information, and the regular views do not?

    A: Because the regular views don't need to know the host, since all generated links are served from the host they link to. Links that show up in emails are not served from the same host (unless you are linking to hotmail.com or gmail.com, etc.)

提交回复
热议问题