[Rails] Implementation of user withdrawal function

Hello, this is tt_tsutsumi. This time I would like to implement the user withdrawal function. I hope this article helps somebody.

I would like to write articles about new user registration and editing from time to time. This time, we will implement the user withdrawal function and prevent the withdrawn users from logging in.

1. Definition of membership status

Set the member status of the user with enum (boolean type). The boolean type is a type that saves boolean values, and only two situations can be registered. This time, we will use this type because the user has two choices: ** valid member or withdrawn member **. Also, if ** is_active ** is ** true (valid member) **, set to log in.

user.rb


    enum is_active: {Available: true, Invalid: false}
    #True for valid members, false for withdrawn members

    def active_for_authentication?
        super && (self.is_active === "Available")
    end
    #is_If active is valid, a valid member(You can log in)

2. Description of route

routes.rb


    resources :users do
        member do
            get "check"
            #Get user membership status
            patch "withdrawl"
            #Update user membership status
        end
    end

3. Create an action

Next, create an action on the controller.

users_controller



def check
    @user = User.find(params[:id])
    #Find user information
end

def withdrawl
    @user = User.find(current_user.id)
    #The currently logged in [email protected] in user
    @user.update(is_active: "Invalid")
    #Change registration information to Invalid with update
    reset_session
    #Reset sessionID
    redirect_to root_path
    #Path to the specified root
end

private

def user_params
    params.require(:user).permit(:active)
end

4. Create a link

Create a link and unsubscribe the user. Since method is updated instead of deleted, it will be described as patch.

ruby:withdrawl.html.erb



<div class="withdrawl">
    <%= link_to "Withdrawal", withdrawl_user_path(@user.id), method: :patch %>
</div>

Now you can implement the user withdrawal function and prevent unsubscribed users from logging in. Thank you for visiting !!

Recommended Posts

[Rails] Implementation of user withdrawal function
[Rails] Implementation of category function
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails] Implementation of user logic deletion
[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
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
Implementation of search function
Implementation of pagination function
[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 fuzzy search function implementation
[Rails] Implement User search function
Implementation of sequential search function
Implementation of like function (Ajax)
Implementation of image preview function
Implementation of category pull-down function
[Rails] Implementation of drag and drop function (with effect)
Implementation of Ruby on Rails login function (devise edition)
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of multi-layer category function using ancestry "seed"
[Rails] Implementation of multi-layer category function using ancestry "Editing form"
[Rails] Implementation of multi-layer category function using ancestry "Creation form"
Rails sorting function implementation (displayed in order of number of like)
[Rails] Implementation of many-to-many category functions
[Rails] gem ancestry category function implementation
[Ruby on Rails] Comment function implementation
[Rails] Comment function implementation procedure memo
Implementation of like function in Java
[Rails] Implementation of new registration function in wizard format using devise
[Rails] Implementation of coupon function (with automatic deletion function using batch processing)
[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it
[Rails] Addition of Ruby On Rails comment function
Rails Addition of easy and easy login function
[Rails withdrawal] Create a simple withdrawal function with rails
[Ruby on Rails] Follow function implementation: Bidirectional
Rails Basic CRUD function implementation procedure scaffold
[Rails] Implementation of validation that maintains uniqueness
[Ruby on Rails] Logical deletion (withdrawal function)
[Rails] I will explain the implementation procedure of the follow function using form_with.
[Rails] Category function
Rails follow function
[Rails] Notification function
Use [Rails] devise Guest user function (for portfolio)
Today's learning summary: With user management function implementation
[JQuery] Implementation procedure of AutoComplete function [Java / Spring]
[Rails] Implementation of "notify notification in some way"
Ruby on Rails Email automatic sending function implementation
Implementation of search function Learning memo (portfolio creation)
[Rails] Implementation of PV number ranking using impressionist
[Rails] Implementation of image slide show using Bootstrap 3
Implementation of delete function (if you have foreign_key)