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チュートリアル11,12章

Posted at

11、12章終わりました

学習時間は4時間程。一周目なので完全に理解できていない。

11章

  • アカウントの有効化
  1. Userモデルに有効化トークンや有効化ステータスを付与する
  2. ユーザー登録
  3. ユーザー認証
  4. アカウント有効化のメールを送信

覚えたこと

どうやらActivartionトークンが必要らしい
既存のユーザーモデルにアカウント有効化のコードを追加

  attr_accessor :remember_token, :activation_token
  before_save   :downcase_email
  before_create :create_activation_digest
  validates :name,  presence: true, length: { maximum: 50 }

メソッドも

# メールアドレスをすべて小文字にする
def downcase_email
  self.email = email.downcase
end

# 有効化トークンとダイジェストを作成および代入する
def create_activation_digest
  self.activation_token  = User.new_token
  self.activation_digest = User.digest(activation_token)
end

そしてテストの準備としてサンプルユーザーを最初から有効にしておく
seed.rbにactivated: true, acivated_at: Time.zone.nowを追記

アカウント有効化のメール送信

送信する為にはAction MailerというライブラリをUserのメイラーに追加するらしい
Userメイラーの生成をして

rails generate mailer UserMailer account_activation password_reset

作られたtextbビューとhtmlビューに表示する内容を書いていく

ちなみに生成されたメイラーのレイアウトはapp/views/layoutsで確認できる

あとはuser_mailer.rbでアカウント有効化リンクを定義して、developmentで試して終了

authenticated?メソッド等重要な点もあるがあくまで記録なので省略

production環境でも出来るらしいが2周目に回します

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?