1
0

More than 1 year has passed since last update.

railsチュートリアル第12章 パスワード再設定のメール送信

Posted at

パスワード再設定のメール送信

createアクションがほぼ動作するところまで持っていきました。
残すところは、パスワード再設定に関するメールを送信する部分です。

パスワード再設定のメールとテンプレート

パスワード再設定のリンクをメール送信する

app/mailers/user_mailer.rb

class UserMailer < ApplicationMailer

  def account_activation(user)
    @user = user
    mail to: user.email, subject: "Account activation"
    # mailにsubjectキーを引数として渡しています。
    # この値は、メールの件名にあたります
  end

  def password_reset
    @user = user
    mail to: user.email, subject: "Password reset"
  end
end

パスワード再設定のテンプレート(テキスト)

app/views/user_mailer/password_reset.text.erb

To reset your password click the link below:

<%= edit_password_reset_url(@user.reset_token, email: @user.email) %>

This link will expire in two hours.

If you did not request your password to be reset, please ignore this email and
your password will stay as it is.

パスワード再設定のテンプレート(HTML)

app/views/user_mailer/password_reset.html.erb

<h1>Password reset</h1>

<p>To reset your password click the link below:</p>

<%= link_to "Reset password", edit_password_reset_url(@user.reset_token,
                                                      email: @user.email) %>

<p>This link will expire in two hours.</p>

<p>
If you did not request your password to be reset, please ignore this email and
your password will stay as it is.
</p>

パスワード再設定のプレビューメソッド(完成)

test/mailers/previews/user_mailer_preview.rb

# Preview all emails at http://localhost:3000/rails/mailers/user_mailer
class UserMailerPreview < ActionMailer::Preview

  # Preview this email at http://rails/mailers/user_mailer/account_activation
  def account_activation
  # http://localhost:3000/rails/mailers/user_mailer/account_activation
    user = User.first
    user.activation_token = User.new_token
    # activation_tokenは仮の属性でしかない
    UserMailer.account_activation(user)
  end

  # Preview this email at http://localhost:3000/rails/mailers/user_mailer/password_reset
  def password_reset
  # http://localhost:3000/rails/mailers/user_mailer/password_reset
    user = User.first
    user.reset_token = User.new_token
    UserMailer.password_reset(user)
  end

end

演習

1.
ブラウザから、送信メールのプレビューをしてみましょう。「Date」の欄にはどんな情報が表示されているでしょうか?
時日が表示されている

2.
パスワード再設定フォームから有効なメールアドレスを送信してみましょう。また、Railsサーバーのログを見て、生成された送信メールの内容を確認してみてください。

確認

3.
コンソールに移り、先ほどの演習課題でパスワード再設定をしたUserオブジェクトを探してください。オブジェクトを見つけたら、そのオブジェクトが持つreset_digestとreset_sent_atの値を確認してみましょう。

>> User.find_by(email: "example@railstutorial.org")
  User Load (0.8ms)  SELECT "users".* FROM "users" WHERE "users"."email" = ? LIMIT ?  [["email", "example@railstutorial.org"], ["LIMIT", 1]]
=> #<User id: 1, name: "Example User", email: "example@railstutorial.org", created_at: "2021-10-17 05:28:48", updated_at: "2021-10-20 04:46:59", password_digest: [FILTERED], remember_digest: nil, admin: true, activation_digest: "$2a$12$x4xVHmtrDFjVPFFWav31UebLY8r9sdT8ImzSXGSrnqt...", activated: true, activated_at: "2021-10-17 05:28:48", reset_digest: "$2a$12$cG8jnT1WjbiqtKZ9ZNgMlOLDZ72VzrIZ2DdB0Nbs2kb...", reset_sent_at: "2021-10-20 04:46:59">

reset_digest: "$2a$12$cG8jnT1WjbiqtKZ9ZNgMlOLDZ72VzrIZ2DdB0Nbs2kb...",
reset_sent_at: "2021-10-20 04:46:59

1
0
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
1
0