yuyanegishi / issue_check

0 stars 0 forks source link

★Rails Turtorial:ユーザー作成〜ユーザーアカウントの有効化 #3

Open yuyanegishi opened 5 years ago

yuyanegishi commented 5 years ago

def create
    @user = User.new(user_params)
    if @user.save
      @user.send_activation_email
      flash[:info] = "Please check your email to activate your account."
      redirect_to root_url
    else
      render 'new'
    end
end
yuyanegishi commented 5 years ago
@user = User.new(user_params)
def user_params
      params.require(:user).permit(:name, :email, :password, :password_confirmation)
end

内容

リファレンス確認 ※Ruby on Rails APIにてrequireで検索。長いので省略

実行結果

params = ActionController::Parameters.new(person: { name: "Finn" })
=><ActionController::Parameters {"person"=>{"name"=>"Finn"}} permitted: false>

name = params.require(:person).require(:name)
=> "Finn"

name
=> "Finn"

params.require(:person).permit(:name)
=> <ActionController::Parameters {"name"=>"Finn"} permitted: true>
yuyanegishi commented 5 years ago
if @user.save

内容

リファレンス確認 ※Ruby on Rails APIにてsaveで検索。長いので省略

Saves the model. If the model is new, a record gets created in the database, otherwise the existing record gets updated.

By default, save always runs validations. If any of them fail the action is cancelled and save returns false, and the record won't be saved. However, if you supply validate: false, validations are bypassed altogether. See ActiveRecord::Validations for more information.

yuyanegishi commented 5 years ago
@user.send_activation_email
#app/models/user.rb
def send_activation_email
    UserMailer.account_activation(self).deliver_now
end
#一部抜粋 app/mailers/user_mail.rb
class UserMailer < ApplicationMailer

  def account_activation(user)
    @user = user
    mail to: user.email, subject: "Account activation"
  end

内容

リファレンス確認 ※Ruby on Rails APIにてmailで検索。長いので省略

mail(headers = {}, &block)

The main method that creates the message and renders the email templates.
There are two ways to call this method, with a block, or without a block.

It accepts a headers hash.
This hash allows you to specify the most used headers in an email message, these are:

":subject"
The subject of the message, if this is omitted, Action Mailer will ask the Rails 
I18n class for a translated :subject in the scope of [mailer_scope, action_name] or
if this is missing, will translate the humanized version of the action_name

":to"
Who the message is destined for, can be a string of addresses, or an array of addresses.

実行結果

yuyanegishi commented 5 years ago

送られるメール内容例 ※logを確認

Hi Chujou Tomoyo,

Welcome to the Sample App! Click on the link below to activate your account:

http://localhost:3000/account_activations/pCjVKiKMZAcMjLGLpWH0kg/edit?email=chujou%40gmail.com

リンクをクリックした後のアクション

class AccountActivationsController < ApplicationController

    def edit
      user = User.find_by(email: params[:email])
      if user && !user.activated? && user.authenticated?(:activation, params[:id])
        user.activate
        log_in user
        flash[:success] = "Account activated!"
        redirect_to user
      else
        flash[:danger] = "Invalid activation link"
        redirect_to root_url
      end
    end

end
yuyanegishi commented 5 years ago
user = User.find_by(email: params[:email])

内容

yuyanegishi commented 5 years ago
if user && !user.activated? && user.authenticated?(:activation, params[:id])
# app/models/user.rb
# トークンがダイジェストと一致したらtrueを返す
def authenticated?(attribute, token)
    digest = send("#{attribute}_digest")
    return false if digest.nil?
    BCrypt::Password.new(digest).is_password?(token)
end

内容

リファレンス確認 ※プロを目指す人のためのRuby入門より

user1.send(:activation_digest) => "$2a$10$gyfsdTN05JAwVft13tnJ2eFuKyODY5k.vydgyiTHb2Jd4PW.BjYNW"

user1.send("activation_digest") => "$2a$10$gyfsdTN05JAwVft13tnJ2eFuKyODY5k.vydgyiTHb2Jd4PW.BjYNW"

yuyanegishi commented 5 years ago
user.activate
def activate
    update_attribute(:activated,    true)
    update_attribute(:activated_at, Time.zone.now)
end

内容

実行確認

# アカウント有効化前
User.find_by(email: "nakayama@gmail.com")
=>#<User id: 104, name: "Nakayama Seika", email: "nakayama@gmail.com", created_at: "2018-11-29 01:54:01", updated_at: "2018-11-29 01:54:01", password_digest: "$2a$10$Omig5jyxxJNq2JkgIuq5ies.M/lOBVCQYnP7Zn/TMzs...", remember_digest: nil, admin: false, activation_digest: "$2a$10$lBKE5DgUb8inZCj/PTRlLulEZ/AhuEvre8DwtJ5aKiE...", activated: false, activated_at: nil, reset_digest: nil, reset_sent_at: nil>

# アカウント有効化後
User.find_by(email: "nakayama@gmail.com")
=>#<User id: 104, name: "Nakayama Seika", email: "nakayama@gmail.com", created_at: "2018-11-29 01:54:01", updated_at: "2018-11-29 01:58:13", password_digest: "$2a$10$Omig5jyxxJNq2JkgIuq5ies.M/lOBVCQYnP7Zn/TMzs...", remember_digest: nil, admin: false, activation_digest: "$2a$10$lBKE5DgUb8inZCj/PTRlLulEZ/AhuEvre8DwtJ5aKiE...", activated: true, activated_at: "2018-11-29 01:58:13", reset_digest: nil, reset_sent_at: nil>