[Ruby on Rails] Logical deletion (withdrawal function)

Target

画面収録 2020-10-25 21.31.39.mov.gif

Development environment

ruby 2.5.7 Rails 5.2.4.3 OS: macOS Catalina

Premise

-[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.

flow

Add 1 column 2 Edit model 3 Edit controller

Add column

Terminal


$ 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.

db/migrate/xxxxxxxxxxxxx_add_is_valid_to_users.rb


class AddIsValidToUsers < ActiveRecord::Migration[5.2]
  def change
    add_column :users, :is_valid, :boolean, default: true, null: false
  end
end

Terminal


$ rails db:migrate

Editing controller

Create unsubscribe screen and unsubscribe action

app/controllers/homes_controller.rb


  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.

app/controllers/users/sessions_controller.rb


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

Edit routes

config/routes.rb


get 'unsubscribe/:name' => 'homes#unsubscribe', as: 'confirm_unsubscribe'
patch ':id/withdraw/:name' => 'homes#withdraw', as: 'withdraw_user'
put 'withdraw/:name' => 'users#withdraw'

Edit view

erb:app/views/homes/unsubscribe.html.erb


<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>

reference

PUT, POST or PATCH?

Summary

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

Recommended Posts

[Ruby on Rails] Logical deletion (withdrawal function)
[Ruby on Rails] Introduced paging function
[Ruby on Rails] CSV output function
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
Ruby on Rails model creation / deletion command
[Ruby on rails] Implementation of like function
Ruby on Rails Elementary
Ruby On Rails Association
Implementation of Ruby on Rails login function (Session)
Ruby on Rails Email automatic sending function implementation
[Ruby on Rails] Post editing function (update, delete)
[Ruby on Rails] Asynchronous communication of posting function, ajax
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Implementation of Ruby on Rails login function (devise edition)
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] Posting score ranking function (whole display)
[Ruby on Rails] about has_secure_password
[Ruby on Rails] Implementation of tagging function/tag filtering function
Ruby on rails learning record-2020.10.07 ②
[Ruby on Rails] Post image preview function in refile
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
[Ruby on Rails] Search function (model, method selection formula)
Ruby on Rails Basic Memorandum
[Ruby on Rails] Bookmark (favorite registration, like) function: One direction
[Rails] Implement event end function (logical deletion) using paranoia (gem)
[Ruby on Rails] Implement login function by add_token_to_users with API
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] Read try (: [],: key)
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
[Rails] Implementation of user withdrawal function
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
(Ruby on Rails6) "Erase" posted content
[Rails] Comment function (registration / display / deletion)
[Ruby on Rails] What is Bcrypt?
[Ruby on Rails] Confirmation page creation
Ruby On Rails devise routing conflict
[Ruby on Rails] Convenient helper method
[Ruby on Rails] Stop "looping until ..."
I want to add a browsing function with ruby on rails
Ruby on Rails for beginners! !! Post list / detailed display function summary
[Ruby on Rails] Posting function that only logged-in users can post
(Ruby on Rails6) Create a function to edit the posted content
[Ruby on Rails] Introduction of initial data