[RUBY] [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 user@Store 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 6] Implementation of search function
[Rails] Implementation of category function
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails] Implementation of user logic deletion
[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
[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
[Vue.js] Implementation of menu function Vue.js introduction rails6
Implementation of search function
Rails search function implementation
Implementation of pagination function
[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 ajax removal
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
Login function implementation with rails
[Rails 6] Pagination function implementation (kaminari)
Ruby on Rails <2021> Implementation of simple login function (form_with)
[Rails] Implementation of drag and drop function (with effect)
Implementation of Ruby on Rails login function (devise edition)
[Ruby on Rails] Implementation of tagging function/tag filtering function
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of multi-layer category function using ancestry "seed"
[Rails] Implementation of SNS authentication (Twitter, Facebook, Google) function
[Rails] Implementation of multi-layer category function using ancestry "Creation form"
Kaminari --Added pagination function of Rails
Rails sorting function implementation (displayed in order of number of like)
[Rails] Implementation of tagging function using intermediate table (without Gem)
[Rails] Implementation of many-to-many category functions
[Rails] gem ancestry category function implementation
[Ruby on Rails] Comment function implementation
[Rails 6] Like function (synchronous → asynchronous) implementation
[Rails] Comment function implementation procedure memo
Implementation of like function in Java
[Rails 6] Implementation of new registration function by SNS authentication (Facebook, Google)
[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