To save the information sent from the view in the User table Specify the instance variable and url of model in form_with.
registrations/new.html.erb
<%= form_with model:@user, url:"/users", local: true do |f| %>
#abridgement
<% end %>
The @user specified in model: here is an instance variable based on the User model, and is declared in the devise controller. The devise controller is usually invisible, but it seems that you can generate it with the following command and customize it.
$ rails g devise:controllers users
The part of URI Pattern set in the following routing is specified in url :.
Prefix Verb URI Pattern Controller#Action
POST /users(.:format) devise/registrations#create
Also, if you want to save the information in a column other than email and password, you must allow saving in the column with the following description. In the example below, keys: [: username] is allowed to save to the username column during sign_up.
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: [:username])
end
end
Reference: https://github.com/heartcombo/devise#strong-parameters
To implement the logout function, prepare a logout link on the view file as shown below.
<%= link_to 'Log out', destroy_user_session_path, method: :delete, class: "logout" %>
It is OK if you set path and method: in the same way as the routing setting.
Prefix Verb URI Pattern Controller#Action
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
To implement the login function, prepare a login link on the view file as shown below.
<%= link_to 'Login', new_user_session_path, class: "login" %>
In the form of the view that prompts you to enter login information, write as follows.
sessions/new.html.erb
<%= form_with model:@user, url:user_session_path, class: 'registration-main',
#abridgement
<% end %>
Send request url: As you can see, When the login link is clicked, the new action of devise / sessions is executed and After entering the login information, when submitted, the create action of devise / sessions will be executed and you can log in.
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
Although it is a brief explanation, the minimum function of the user authentication function using devise has been implemented. Thank you for visiting.
Recommended Posts