Gemfile
:
gem 'devise' #add to
terminal
$ bundle install
terminal
$ rails g devise:install
$ rails g devise User name:string #Creating a model
$ rails db:migrate
$ rails g devise:views users #Create view
$ rails g devise:controllers users #Creating a controller
Since the name column does not exist by default in the model created with rails g devise" model name "
, create the column at the same time as described above, or describe it in the migration file and reflect it in the table.
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
end
config/routes.rb
...Edit
devise_for :users #Declaration to include users in the URL when using the devise feature
↓
devise_for :users, controllers: {
sessions: 'users/sessions', #Use the sessions controller under the users directory
registrations: 'users/registrations' #Use the registrations controller under the users directory
}
The above controllers: {}
specification changes the routing settings as follows:
terminal
devise/sessions#create
↓
users/sessions#create
Added name input form to new registration screen
erb:app/views/users/registrations/new.html.erb
... +Add part of
+ <div class="field">
+ <%= f.label :name %><br />
+ <%= f.text_field :name, autofocus: true, autocomplete: "name" %>
+ </div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
:
Changed the login screen input form from email
to name
** This will change to email login authentication **
erb:app/views/users/sessions/new.html.erb
...Edit
:
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
↓
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true, autocomplete: "name" %>
</div>
:
app/controllers/application_controller.rb
...add to
before_action :configure_permitted_parameters,if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up,keys:[:name])
end
When the devise usage functions (user registration, login authentication, etc.) are used, configure_permitted_parameters
is executed before that.
devise_parameter_sanitizer.permit(:sign_up,keys[:name])
= Allow data manipulation of user name (name) at the time of user registration (sign_up)
Similar functionality to Strong Parameters
** · private = See only within your controller **
** · protected = Can be referenced by other called controllers **
erb:app/views/layouts/application.html.erb
...+Add part
<body>
+ <% if user_signed_in? %>
+ <%= link_to "Logout", destroy_user_session_path, method: :delete %>
+ <% else %>
+ <%= link_to "sign up", new_user_registration_path %>
+ <%= link_to "Login", new_user_session_path %>
+ <% end %>
:
that's all
** We would appreciate it if you could point out any mistakes in the description. We are looking forward to hearing from you. ** **
Recommended Posts