Open yuyanegishi opened 5 years ago
@user = User.new(user_params)
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
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>
if @user.save
save(*args)
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.
@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
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.
ApplicationMailer.superclass
=> ActionMailer::Base
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
user = User.find_by(email: params[:email])
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
Compares a potential secret against the hash. Returns true if the secret is the original secret, false otherwise.
user1 = User.first
=> #<User id: 1, name: "Example User", email: "example@railstutorial.org", created_at: "2018-11-22 01:14:16", updated_at: "2018-11-22 01:14:16", password_digest: "$2a$10$V2dr6oBhIMNNoQHjpj82EO9TBCMoiZz8a9oDKrLTB7o...", remember_digest: nil, admin: true, activation_digest: "$2a$10$gyfsdTN05JAwVft13tnJ2eFuKyODY5k.vydgyiTHb2J...", activated: true, activated_at: "2018-11-22 01:14:16", reset_digest: nil, reset_sent_at: nil>
user1.send(:activation_digest) => "$2a$10$gyfsdTN05JAwVft13tnJ2eFuKyODY5k.vydgyiTHb2Jd4PW.BjYNW"
user1.send("activation_digest") => "$2a$10$gyfsdTN05JAwVft13tnJ2eFuKyODY5k.vydgyiTHb2Jd4PW.BjYNW"
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>