In Rails, I summarized how to implement user management function using devise of gem.
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.
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.
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>
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