[Rails] Implementation of user logic deletion

Target

ezgif.com-video-to-gif.gif

Development environment

・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina

Premise

The following has been implemented.

Slim introductionIntroduction of Bootstrap3 -Login function implementationDevise Japanese localization

Implementation

1. Add column

Terminal


$ rails g migration AddIsValidToUsers is_valid:boolean

~_add_is_valid_to_users.rb


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

Terminal


$ rails db:migrate

2. Edit the model

user.rb


#Postscript
enum is_valid: { 'Effectiveness': true, 'Withdrawn': false }

def active_for_authentication?
  super && self.is_valid == 'Effectiveness'
end

[Explanation]

(1) Manage the user status with enum.

enum is_valid: { 'Effectiveness': true, 'Withdrawn': false }

(2) If is_valid is valid, define a method that returns true.

def active_for_authentication?
  super && self.is_valid == 'Effectiveness'
end

3. Edit session_controller.rb

session_controller.rb


#Postscript
protected

  def reject_user
    user = User.find_by(email: params[:user][:email].downcase)
    if user
      if (user.valid_password?(params[:user][:password]) && (user.active_for_authentication? == true))
        redirect_to new_user_session_path
      end
    end
  end

[Explanation]

(1) Check if there is a user corresponding to the entered e-mail address.

user = User.find_by(email: params[:user][:email].downcase)

(2) If the entered password is correct and the return value of the method defined in 2 is true, the login screen is displayed without performing login processing.

if (user.valid_password?(params[:user][:password]) && (user.active_for_authentication? == true))
  redirect_to new_user_session_path
end

4. Edit the view

Display flash messages using the Bootstrap 3 alert component.

sessions/new.html.slim


/Postscript
- if flash.present?
  .alert.alert-danger.alert-dismissible.fade.in role='alert'
    button.close type='button' data-dismiss='alert'
      span aria-hidden='true'
        | ×
    - flash.each do |name, msg|
      = content_tag :div, msg, :id => 'flash_#{ name }' if msg.is_a?(String)

      p
        a href='#' data-dismiss='alert'
          |close

Recommended Posts

[Rails] Implementation of user logic deletion
[Rails] Implementation of user withdrawal function
Rails implementation of ajax removal
[Rails] Implementation of category function
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
[Rails] Implementation of image preview function
[Rails] About implementation of like function
[Rails] Implementation of CSV export function
[Rails] Implementation of many-to-many category functions
[Rails] Implementation of coupon function (with automatic deletion function using batch processing)
Implementation of user authentication function using devise (2)
Implementation of user authentication function using devise (1)
Rails [For beginners] Implementation of comment function
Implementation of user authentication function using devise (3)
[Ruby on rails] Implementation of like function
[Rails] Implementation of validation that maintains uniqueness
[Rails] Implementation of search function using gem's ransack
Implementation of Ruby on Rails login function (Session)
[Rails] Implementation of image enlargement function using lightbox2
[Rails] Implementation of "notify notification in some way"
[Rails] Implementation of PV number ranking using impressionist
[Rails] Implementation of image slide show using Bootstrap 3
Rails hashtag search implementation
Rails6 countdown timer implementation
[rails] gem'payjp' implementation procedure
[Rails] Implementation of drag and drop function (with effect)
Implementation of Ruby on Rails login function (devise edition)
[Rails] Introduction of PAY.JP
Rails Action Text implementation
Implementation of search function
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
Applied implementation of chat-space
[Rails] Implementation of multi-layer category function using ancestry "seed"
Implementation of pagination function
Introduction of user authentication
[Rails6] devise + paranoia + both user logic deletion and unique constraints realized in MySQL8 series
Rails CRUD function implementation ① (new addition, deletion this time)
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
Build a bulletin board API with authentication and authorization with Rails # 18 ・ Implementation of final user controller
[Rails] Implementation of multi-layer category function using ancestry "Editing form"
[Rails] Implementation of automatic address input using jpostal and jp_prefecture
[Rails] Implementation of multi-layer category function using ancestry "Creation form"
[Ruby on Rails] Elimination of Fat Controller-First, logic to model-
Rails sorting function implementation (displayed in order of number of like)
Rails fuzzy search function implementation
[Rails] Introduction of devise Basics
[Rails] Implement User search function
Implementation of sequential search function
Rails Posts and User Linkage
Utilization of Rails Boolean type
Implementation of like function (Ajax)
Implementation of image preview function
[Java] Implementation of Faistel Network
Implementation of XLPagerTabStrip with TabBarController
Implementation of category pull-down function
Implementation of tri-tree in Java
Implementation of HashMap in kotlin
[Rails] Implementation of new registration function in wizard format using devise