Implementation of Ruby on Rails login function (devise edition)


Implement login function using gem'devise' Change to name and password login instead of password and email login.

This goal and my environment

--Implemented login function using devise --Change to login with name and password




#Added at the bottom
gem 'devise'

Execute the command below the terminal after editing the Gemfile


bundle install
rails g devise:install
rails g devise User
rails db:migrate

However, this time I decided to log in by name, so edit the migration file before rails db: migrate.


#t.timestamps null:Add above false
t.string :name

Create view


rails g devise:view

By the way, in the created file, registrations is a new registration and sessions is a file related to login.


<h2>Log in</h2>

<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name, autofocus: true, autocomplete: "name" %>

(Omitted as there is no change after this)
<% end %>
<%= render "devise/shared/links" %>

Change email to name and email_field to text_field


#Added above the email div tag
<div class="field">
  <%= f.label :name %><br />
  <%= f.text_field :name, autofocus: true, autocomplete: "name" %>

Allow parameters


class ApplicationController < ActionController::Base
    before_action :configure_premitted_parameters, if: :devise_controller?


        def configure_premitted_parameters
            devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :email])
            devise_parameter_sanitizer.permit(:sign_in, keys: [:name])

Almost completed with this

Screen production after login


rails g controller home top


<%= link_to "Log out", destroy_user_session_path, method: :delete %>

I want to be able to log out, so create a logout button using the link_to method. Add the following to application_controller.rb


#Add on top of protected
def after_sign_in_path_for(resource)


#Uncomment the following around line 49
config.authentication_keys = [:email]
#Change the above email to name
config.authentication_keys = [:name]

Please note that you cannot log in by name without this!

that's all.

Recommended Posts

Implementation of Ruby on Rails login function (devise edition)
Implementation of Ruby on Rails login function (Session)
Ruby on Rails <2021> Implementation of simple login function (form_with)
[Ruby on rails] Implementation of like function
[Ruby on Rails] Implementation of tagging function/tag filtering function
[Ruby on Rails] Comment function implementation
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] Follow function implementation: Bidirectional
Validation settings for Ruby on Rails login function
Ruby on Rails Email automatic sending function implementation
[Note] Summary of rails login function using devise ①
[Rails 6] Implementation of search function
[Rails] Implementation of category function
Login function implementation with rails
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
Rails tutorial (6th edition) Background operation of login function
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
[rails] Login screen implementation in devise
[Ruby on Rails] Introduced paging function
[Rails] Implementation of image preview function
Basic knowledge of Ruby on Rails
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
[Ruby on Rails] CSV output function
Ruby On Rails devise routing conflict
[Ruby on Rails] DM, chat function
A note about the seed function of Ruby on Rails
[Ruby on Rails] Implement login function by add_token_to_users with API
[Apple login] Sign in with Apple implementation procedure (Ruby on Rails)
[Rails] Implementation of new registration function in wizard format using devise
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Search function (not selected)
[Rails] Function restrictions in devise (login / logout)
Ruby on Rails installation method [Mac edition]
Rails Addition of easy and easy login function
Let's summarize "MVC" of Ruby on Rails
part of the syntax of ruby ​​on rails
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)
[Ruby on Rails] Japanese notation of errors
(2021) Ruby on Rails administrator (admin) login creation
Explanation of Ruby on rails for beginners ①
[Vue.js] Implementation of menu function Implementation version rails6
[Vue.js] Implementation of menu function Vue.js introduction rails6
[Ruby on Rails] Logical deletion (withdrawal function)
[Rails] Implementation of search function using gem's ransack
[Rails 6] Implementation of inquiry function using Action Mailer
[Ruby on Rails] Until the introduction of RSpec
[Rails] Implementation of image enlargement function using lightbox2
Recommendation of Service class in Ruby on Rails
[Rails] Implementation of retweet function in SNS application
Ruby on Rails ~ Basics of MVC and Router ~
[Ruby on Rails] A memorandum of layout templates
Ruby on Rails address automatic input implementation method
[Ruby on Rails] Post editing function (update, delete)