简体   繁体   中英

ruby on rails - use a helper in html.erb

I have a html.erb file used for an email template. Is there any way to include a helper module and use it's methods within this file? Something like:

a mails_helper.rb file:

module MailsHelper
  def mail_to
    "foo"
  end
end

and in mail_template.html.erb :

<% include MailsHelper %>
  <h2> This mail was sent to: <%= mail_to %> </h2>

Add helper :mail to the top of your ActionMailer derived class
ex:- app/mailers/mails_mailer.rb

class MailMailer < ActionMailer::Base
  helper :mails

  ...
end

Checkout this answer

Name the helper file with same prefix as the ActionMailer's file name.

For example: mailer: users_mailer.rb helper: users_mailer_helper.rb

According to the ruby on rails naming conventions - any view rendered by UsersMailer would have access to your helper methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM