[RUBY] [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 6] Implementation of search function
[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
[Rails 6] Implementation of SNS (Twitter) sharing function
Implementation of user authentication function using devise (3)
[Vue.js] Implementation of menu function Implementation version rails6
[Ruby on rails] Implementation of like function
[Rails] Implementation of validation that maintains uniqueness
[Vue.js] Implementation of menu function Vue.js introduction rails6
Implementation of GKAccessPoint
[Rails] Implementation of search function using gem's ransack
Implementation of Ruby on Rails login function (Session)
[Rails 6] Implementation of inquiry function using Action Mailer
[Rails] Implementation of image enlargement function using lightbox2
[Rails] Implementation of retweet function in SNS application
[Rails] Implementation of "notify notification in some way"
[Rails] Implementation of batch processing using whenever (gem)
[Rails] Implementation of PV number ranking using impressionist
[Rails] Implementation of image slide show using Bootstrap 3
Rails hashtag search implementation
Ruby on Rails <2021> Implementation of simple login function (form_with)
Rails6 countdown timer implementation
[rails] gem'payjp' implementation procedure
Implementation of flash messages
[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
[Ruby on Rails] Implementation of tagging function/tag filtering function
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
Rails Tutorial/Significance of Indexing
Applied implementation of chat-space
[Rails] Implementation of multi-layer category function using ancestry "seed"
Rails search function implementation
Implementation of pagination function
Introduction of user authentication
[Rails] Implementation of SNS authentication (Twitter, Facebook, Google) function
[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)