ruby 2.5.7 Rails 184.108.40.206 OS: macOS Catalina
-[Ruby on Rails] How to log in with only your name and password using gem devise -[Ruby on Rails] Change URL id to column name We will continue to edit the code.
Add 1 column 2 Edit model 3 Edit controller
$ rails g migration AddIsValidToUsers is_valid:boolean
Added default: true and null: false. By using boolean, it is determined whether or not you have unsubscribed with ture or false. In the following cases, the initial value is set to true, so if you have already withdrawn, it will be false.
class AddIsValidToUsers < ActiveRecord::Migration[5.2] def change add_column :users, :is_valid, :boolean, default: true, null: false end end
$ rails db:migrate
Create unsubscribe screen and unsubscribe action
def unsubscribe @user = User.find_by(name: params[:name]) end def withdraw @user = User.find_by(name: params[:name]) @user.update(is_valid: false) reset_session redirect_to root_path end
A description that prevents login after withdrawal.
class Users::SessionsController < Devise::SessionsController before_action :reject_inactive_user, only: [:create] ... def reject_inactive_user @user = User.find_by(name: params[:user][:name]) if @user if @user.valid_password?(params[:user][:password]) && [email protected]_valid redirect_to new_user_session_path end end end end
get 'unsubscribe/:name' => 'homes#unsubscribe', as: 'confirm_unsubscribe' patch ':id/withdraw/:name' => 'homes#withdraw', as: 'withdraw_user' put 'withdraw/:name' => 'users#withdraw'
<div> <h2>Do you really want to unsubscribe?</h2> <div> <p>To unsubscribe, click "Unsubscribe".</p> </div> <div> <%= link_to 'Do not withdraw', mypage_path(@user) %> <%= link_to "Withdraw", withdraw_user_path(@user), method: :patch %> </div> </div>
I think there are various advantages and disadvantages of logical deletion, Customer information is important when operating a service, so It is better to unsubscribe by logical deletion instead of physical deletion It is recommended because it can be used as a means of revival.
Also, on twitter, technologies and ideas that have not been uploaded to Qiita are also uploaded, so I would be grateful if you could follow me. Click here for details https://twitter.com/japwork