Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@unidon

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

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メソッドを使いましょう。

以上

0
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
unidon
田舎でweb開発と釣りをして暮らしています。 java, ruby on rails, jQuery, react, AWS...etc

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?