0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Railsチュートリアル(第12章)

Last updated at Posted at 2019-08-28

はじめに

Railsチュートリアルの第12章が終わりました。
この章では、パスワード再設定を行います。
メールを送って再設定用のリンクにアクセスする基本的な流れは11章と同じです。
ポイントだけメモしておきます。

隠しフィールド

メールアドレスからユーザーを特定するために、メールで送信したリンクからアクセスした際のeditアクションと、その後のパスワード再設定フォームからの送信時のupdateアクションでそれぞれメールアドレスが必要です。
リンクからアクセスする際は、リンクのパラメータにメールアドレスを含めていますが、フォームの送信で消えてしまいます。そのために、フォーム内に隠しフィールドとして保持します。

以下のように、hidden_field_tagで記述します。

    <%= form_for(@user, url: password_reset_path(params[:id])) do |f| %>
      ...
      <%= hidden_field_tag :email, @user.email %>
      ...
    <% end %>

f.hidden_fieldを使用しないのは、以下のようにパラメータの取得の仕方が変わるからです。

hidden_field_tag :email, @user.email # params[:email] 
f.hidden_field :email, @user.email   # params[:user][:email] 

時間の比較

時間も数値等と同じように比較することができます。
以下のように記述することで、reset_sent_atが2時間より前かどうか判定しています。

reset_sent_at < 2.hours.ago

考え方としては、reset_sent_at2.hour.ago(2時間前の時間)より前であればtrueです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?