Added a gem called devise to the gemfile.
gemfile
gem 'devise'
In the terminal, move to the corresponding directory (sample this time) and After installing Gem with "bundle install" Run "rails g devise: install" to create a devise config file.
Terminal
$ cd ~/projects/sample
$ bundle install
$ rails g devise:install
** Newly created file **
If you can confirm that the file has been created, Let's create a User model with the devise model creation command.
Terminal
rails g devise user
** Newly created file **
In addition, the following routes are automatically added to route.rb.
config/routes.rb
devise_for :users
devise_for is a devise method that generates the routing required for user functions at once.
If you want to add a nickname column to the users table, add it to the migration file. If you want to add a function to search by nickname, paste the index as well.
Migration file
class DeviseCreateUsers < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
## Database authenticatable
t.string :nickname, null: false
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
#~abridgement~
end
#Later, when implementing the search function and searching with the nickname column, add the following as well.
add_index :users, :nickname, unique: true
#~abridgement~
Make sure to validate the model file as well.
app/models/user.rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
validates :nickname, presence: true, uniqueness: true
end
Then execute the migration in the terminal.
Terminal
rails db:migrate
When the login function is implemented using devise, how to receive the parameters is different from usual. Strong parameters that limit the parameters sent at login cannot be edited because they are described in the devise Gem. Therefore, use the "** configure_permitted_parameters method **" provided by devise to allow the form to receive the value of the nickname column.
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname])
end
end
Now that you've allowed the value to be sent to the nickname column, modify the view file to add the nickname form to the sign-up screen. To make changes to the view file, you need to use the devise command to generate the view file.
Terminal
$ rails g devise:views
With the above work, we were able to generate a view file for devise. Edit the generated view file to add a form for nickname to the sign-up screen and change the layout to your liking.
The sign-up screen is
The login screen is
The view file is supported.
This is the end of the explanation of the introduction of devise.
Recommended Posts