【Rails】사용자 논리 삭제 구현
목표
 
 개발 환경
· Ruby : 2.5.7
·Rails: 5.2.4
·Vagrant: 2.2.7
· VirtualBox : 6.1
· OS : macOS Catalina
 전제
하기 실장 완료.
· Slim 도입
· Bootstrap3 도입
· 로그인 기능 구현
· devise 일본어화
 구현
 1. 컬럼 추가
터미널$ rails g migration AddIsValidToUsers is_valid:boolean
~_add_is_valid_to_users.rbclass AddIsValidToUsers < ActiveRecord::Migration[5.2]
  def change
    # 「default: true」と「null: false」を追記
    add_column :users, :is_valid, :boolean, default: true, null: false
  end
end
터미널$ rails db:migrate
 2. 모델 편집
user.rb# 追記
enum is_valid: { '有効': true, '退会済': false }
def active_for_authentication?
  super && self.is_valid == '有効'
end
 【해설】
 ① 사용자의 상태를 enum으로 관리한다.
enum is_valid: { '有効': true, '退会済': false }
 ② is_valid가 유효하면 true를 반환하는 메소드를 정의한다.
def active_for_authentication?
  super && self.is_valid == '有効'
end
 3. session_controller.rb 편집
session_controller.rb# 追記
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
 【해설】
 ① 입력된 이메일 주소에 해당하는 사용자가 존재하는지 확인한다.
user = User.find_by(email: params[:user][:email].downcase)
 ② 입력된 패스워드가 올바르고 2로 정의한 메소드의 반환값이 true이면 로그인 처리를 하지 않고 로그인 화면으로 천이한다.
if (user.valid_password?(params[:user][:password]) && (user.active_for_authentication? == true))
  redirect_to new_user_session_path
end
 4. 뷰 편집
Bootstrap3의 경고 구성 요소를 사용하여 플래시 메시지를 표시합니다.
sessions/new.html.slim/ 追記
- 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'
          | 閉じる
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(【Rails】사용자 논리 삭제 구현), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/matsubishi5/items/0c442750c6ac4033888e
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                 우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
· Ruby : 2.5.7
·Rails: 5.2.4
·Vagrant: 2.2.7
· VirtualBox : 6.1
· OS : macOS Catalina
전제
하기 실장 완료.
· Slim 도입
· Bootstrap3 도입
· 로그인 기능 구현
· devise 일본어화
 구현
 1. 컬럼 추가
터미널$ rails g migration AddIsValidToUsers is_valid:boolean
~_add_is_valid_to_users.rbclass AddIsValidToUsers < ActiveRecord::Migration[5.2]
  def change
    # 「default: true」と「null: false」を追記
    add_column :users, :is_valid, :boolean, default: true, null: false
  end
end
터미널$ rails db:migrate
 2. 모델 편집
user.rb# 追記
enum is_valid: { '有効': true, '退会済': false }
def active_for_authentication?
  super && self.is_valid == '有効'
end
 【해설】
 ① 사용자의 상태를 enum으로 관리한다.
enum is_valid: { '有効': true, '退会済': false }
 ② is_valid가 유효하면 true를 반환하는 메소드를 정의한다.
def active_for_authentication?
  super && self.is_valid == '有効'
end
 3. session_controller.rb 편집
session_controller.rb# 追記
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
 【해설】
 ① 입력된 이메일 주소에 해당하는 사용자가 존재하는지 확인한다.
user = User.find_by(email: params[:user][:email].downcase)
 ② 입력된 패스워드가 올바르고 2로 정의한 메소드의 반환값이 true이면 로그인 처리를 하지 않고 로그인 화면으로 천이한다.
if (user.valid_password?(params[:user][:password]) && (user.active_for_authentication? == true))
  redirect_to new_user_session_path
end
 4. 뷰 편집
Bootstrap3의 경고 구성 요소를 사용하여 플래시 메시지를 표시합니다.
sessions/new.html.slim/ 追記
- 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'
          | 閉じる
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(【Rails】사용자 논리 삭제 구현), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/matsubishi5/items/0c442750c6ac4033888e
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                 우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
1. 컬럼 추가
터미널
$ rails g migration AddIsValidToUsers is_valid:boolean
~_add_is_valid_to_users.rb
class AddIsValidToUsers < ActiveRecord::Migration[5.2]
  def change
    # 「default: true」と「null: false」を追記
    add_column :users, :is_valid, :boolean, default: true, null: false
  end
end
터미널
$ rails db:migrate
2. 모델 편집
user.rb
# 追記
enum is_valid: { '有効': true, '退会済': false }
def active_for_authentication?
  super && self.is_valid == '有効'
end
【해설】
① 사용자의 상태를 enum으로 관리한다.
enum is_valid: { '有効': true, '退会済': false }
② is_valid가 유효하면 true를 반환하는 메소드를 정의한다.
def active_for_authentication?
  super && self.is_valid == '有効'
end
3. session_controller.rb 편집
session_controller.rb
# 追記
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
【해설】
① 입력된 이메일 주소에 해당하는 사용자가 존재하는지 확인한다.
user = User.find_by(email: params[:user][:email].downcase)
② 입력된 패스워드가 올바르고 2로 정의한 메소드의 반환값이 true이면 로그인 처리를 하지 않고 로그인 화면으로 천이한다.
if (user.valid_password?(params[:user][:password]) && (user.active_for_authentication? == true))
  redirect_to new_user_session_path
end
4. 뷰 편집
Bootstrap3의 경고 구성 요소를 사용하여 플래시 메시지를 표시합니다.
sessions/new.html.slim
/ 追記
- 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'
          | 閉じる
Reference
이 문제에 관하여(【Rails】사용자 논리 삭제 구현), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/matsubishi5/items/0c442750c6ac4033888e텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)