RailsTutrial11章

紹介

アカウント有効化

トークンとメールアドレスの情報の2つをURLに含める。 (URLパラメータとか) 色々含んだURLを盛り込む。

f:id:shiness:20210115123946p:plainf:id:shiness:20210115124024p:plainf:id:shiness:20210115124034p:plainf:id:shiness:20210115124059p:plain

activation

したのかしてないのかのカラムも必要

f:id:shiness:20210115125150p:plain

editアクションがちょうどよい。/:id が入れられるから showアクションはリソースを変更しないから普通。 Restfulの規則的にはeditの方がふさわしいので。

sessionのときと同じかんじ

    def create_activation_digest
      self.activation_token  = User.new_token
      self.activation_digest = User.digest(activation_token)
    end

attr_accessorに追加 before_create : userがsign_upされる直前に必要。

ActionMailerについて

View → Mail Controller → Mailer

f:id:shiness:20210115141255p:plain

 メールの中に何を含めるか

f:id:shiness:20210115141607p:plain

 actionと違って引数がとれるメソッドと呼ぶべき コントローラを継承しているけど

 => return: mail object (text/html) example: mail.deliver

アカウント有効化のプレビューメソッド

 def account_activation
    user = User.first
    user.activation_token = User.new_token
    UserMailer.account_activation(user)
  end

signup時のcreateアクションを更新

f:id:shiness:20210115153206p:plain

オマケトピックス

9章を参考に作ってもよいが @user.authenticated?を便利にしたい!

f:id:shiness:20210115154406p:plain

メソッドを使い分けたいときどうするか?

エラーになる。以下のことを実現したい。

@user.remember_diggest

上に対して
a = "remember"

@user.#{a}digest @user"#{a}digest"

rubyの機能に`send`メソッドがある。
"文字列でも書ける"
メソッド名が一致していないとこの前提条件を満たすことができない!!!
未来を予想して作らないと無理
`メタプログラミングの序章`

attribute = :activation user.send("#{attribute}_digest")

nilじゃないか確認&2回目のクリックしたとかがないか& <span style="color: #62c7c9">params[:id]となっているけどこれはトークンが入っている</span>

  if user && !user.activated? && user.authenticated?(:activation, params[:id]) user.update_attribute(:activated, true) user.update_attribute(:activated_at, Time.zone.now)


# 目立たないバグ

 activation前にログインできてしまう問題


# リファクタリング

   メソッド化する

UserMailer.accont...これがメールに飛ぶっていうのがわかりにくいのでメソッド化する