LoginSignup
3
3

More than 3 years have passed since last update.

ActionMailerのViewでHelperのメソッドを使う

Last updated at Posted at 2021-04-08

ActionMailerを使用したメールの文面でHelperのメソッドを使いたい時の例

helperメソッドを使います。
https://api.rubyonrails.org/v6.1.0/classes/ActionController/Helpers.html

text_helper.rb
module TextHelper
  def change_text(text)
    ・・・ #テキスト変更する処理
  end
end
user_mailer.rb
class UserMailer < ActionMailer::Base
  helper TextHelper

  def mail_to_user(user)
    @user = user
    mail(to: user.email, subject: 'Title')
  end
end
mail_to_user.text.rb
<%= change_text(@user.name) %>

こんな感じで行ける。

おまけ

ActionMailer Helperでググると

add_template_helper(ApplicationHelper)

を追加するって言う記事がでてくるけど
Rails 6.1で削除され動かなくなるようなので注意が必要です。
helperメソッドを使いましょう。

以上

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3