[RUBY] Introduce devise in Rails to implement user management functionality

Introduce devise and easily implement user management functions

In Rails, I summarized how to implement user management function using devise of gem.

Introducing devise

First, add devise to the gemfile.

gemfile


gem 'devise'

Run bundle install ** and restart the server. ** **

Then create a devise config file.

$ rails g devise:install

Create a user model.

rails g devise user

At this time, the migration file will be generated at the same time as the user model is created, so if there is a column to add, describe it before migrating.

○○_devise_create_users.rb


--abridgement--
class DeviseCreateUsers < ActiveRecord::Migration[6.0]
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :nickname,           null: false, default: ""
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""
      t.string :family_name,        null: false, default: ""
      t.string :first_name,         null: false, default: ""
      t.string :read_family,        null: false, default: ""
      t.string :read_first,         null: false, default: ""
      t.date :birth,                null: false
--abridgement--

Email and password should be attached by default, so this time I created to add other columns.

As an aside, when preparing the date of birth column, I use the date type, but since the default value can not be set other than a constant, I have not set the default this time.

Migrate when you're ready.

$ rails db:migrate

All you have to do is prepare the view file and you're ready to go.

Sign-up implementation

In order to register information other than email and password when user signs up, it is necessary to describe in application_controller.

application_controller.rb


class ApplicationController < ActionController::Base
  before_action :authenticate_user!, except: [:index]
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected
  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname, :family_name, :email, :first_name, :read_family, :read_first, :birth])
  end
end

If you are transitioning to a page other than when you are on the index page, you can force yourself to jump to the login page. I wrote the before_action: authenticate_user! method.

To register additional parameters Before_action: Write the configure_permitted_parameters method.

Also, be sure to pass the model instance variables and paths to the form_with method of the view file.

ruby:views/devise/registrations/new.html.erb


---abridgement---
<%= form_with class: 'registration-main', model: @user, url: user_registration_path,  local: true do |f| %>
---abridgement---

ruby:views/devise/sessions/new.html.erb


---abridgement---
<%= form_with class: 'registration-main',model: @user, url: user_session_path, local: true do |f| %>
---abridgement---

Without it, it wouldn't work well with the user table.

Logout implementation

Here, let's implement a logout button in the view file with the link_to method. The point is to remember to write the path to disconnect the user session.

ruby:xxx.html.erb


<li><%= link_to 'Log out', destroy_user_session_path, method: :delete, class: "logout" %></li>

How to change the notation between login and guest

When you are logged in, a button to move to My Page and a button to log out are displayed on the page, I will also summarize the description so that the new registration button and login button will be displayed when you are not logged in (when you are a guest).

Let's use the user_signed_in? method of the helper method. Write the following in the corresponding view file.

ruby:views/xxx.html.erb


<% if user_signed_in? %>
          <li><%= link_to current_user.nickname, root_path, class: "user-nickname" %></li>
          <li><%= link_to 'Log out', destroy_user_session_path, method: :delete, class: "logout" %></li>
      <% else %>
          <li><%= link_to 'Login', new_user_session_path, class: "login" %></li>
          <li><%= link_to 'sign up', new_user_registration_path, class: "sign-up" %></li>
      <% end %>

Recommended Posts

Introduce devise in Rails to implement user management functionality
Implement user management functionality using Devise
How to implement search functionality in Rails
How to implement ranking functionality in Rails
How to introduce jQuery in Rails 6
Implement user registration function and corporate registration function separately in Rails devise
How to update user edits in Rails Devise without entering a password
[Rails] How to get the user information currently logged in with devise
[Rails] Use devise to get information about the currently logged in user
Implement user management functions in a wizard format
How to implement a like feature in Rails
[Rails] Added in devise: username not added to database
Introduce Vue.js to Rails
Implement markdown in Rails
How to implement guest login in 5 minutes in rails portfolio
How to implement a like feature in Ajax in Rails
Implement user follow function in Rails (I use Ajax) ②
Implement user follow function in Rails (I use Ajax) ①
Implement application function in Rails
[Rails] Implement User search function
Implement follow function in Rails
[Rails] How to install devise
Implement LTI authentication in Rails
[Rails] Add column to devise
Implement import process in Rails
[Rails] How to implement scraping
Memorandum [Rails] User authentication Devise
Super easy in 2 steps! How to install devise! !! (rails 5 version)
[Rails] How to use gem "devise"
[Rails] A simple way to implement a self-introduction function in your profile
[Rails] How to use devise (Note)
I tried to implement Ajax processing of like function in Rails
[Rails] Session timeout setting in devise
[rails] Login screen implementation in devise
[Rails] How to write in Japanese
Until you introduce fonts to Rails
Implement simple login function in Rails
Implement a contact form in Rails
I want to add devise in Rails, but I can't bundle install
[Rails] Add strong parameters to devise
[Rails] How to implement star rating
Try to implement Yubaba in Java
Implement CSV download function in Rails
How to install Swiper in Rails
How to implement a slideshow using slick in Rails (one by one & multiple by one)
How to implement more than one top page in Rails breadcrumb trail
How to implement gem "summer note" in wysiwyg editor in Ruby on Rails
How to implement date calculation in Java
How to implement Kalman filter in Java
[Rails] Function restrictions in devise (login / logout)
How to change app name in rails
I tried to introduce CircleCI 2.0 to Rails app
How to use custom helpers in rails
Try to implement n-ary addition in Java
Introduce two-factor authentication to your Rails application
How to insert a video in Rails
How to use MySQL in Rails tutorial
How to implement coding conventions in Java
Steps to set a favicon in Rails
Preparing to introduce jQuery to Ruby on Rails
[rails] How to configure routing in resources