devise is a gem that makes it easy to implement user management functions. Because it made it easy, there were many parts where I didn't understand "why it works". I'm creating an original application, I want to change the specifications, and after a lot of trial and error, I feel that I've learned how to use devise more than before.
When I first learned about devise, I didn't deal with controllers at all. For devise, the routing was generated without permission, and the controller didn't even exist, even though other functions were implemented with the MVC model in mind. Since Rails is singing the MVC model, I've been learning with the idea of "I wonder if there is a controller, but I wonder if it's disappearing." When I check the command in the terminal,
rails g --help
=>
Devise:
devise
devise:controllers
devise:install
devise:views
I noticed that devise can generate a controller.
The strong parameter was set in application_controller.rb
. This file is inherited by all controllers, so it also applies to devise's controllers.
To avoid interfering with controllers other than devise
application_controller.rb
before_action :configure_permitted_parameters, if: :devise_controller?
private
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:Column name])
end
I wrote that, and only when using the devise controller with devise_controller ?, the processing of the strong parameter was done.
In the application I'm currently writing, I'm wondering if I can change the user's sign-in to "nickname" </ font> and "password" instead of authenticating with "email address" and "password". It was.
At the terminal
rails g devise:controllers user
The difference from the generation of other controllers is that they are pluralized as controller s </ font>. This is because this command will generate various controllers at the same time, such as devise sign-in and sign-up.
config/initializwes/devise.rb/Line 49
config.authentication_keys = [:nickname]
Uncomment and describe the column you want to use for any authentication.
: password
.app/controllers/user/sessions_controller.rb
class Users::SessionsController < Devise::SessionsController
before_action :configure_sign_in_params, only: [:create] #Uncomment
(Omission)
protected #Uncomment
# If you have extra params to permit, append them to the sanitizer.
def configure_sign_in_params #Uncomment
devise_parameter_sanitizer.permit(:sign_in, keys: [:nickname, :password]) #Uncomment and describe the column you want to use for authentication in square brackets * Also describe password here
end #Uncomment
config/routes.rb
devise_for :users, controllers: {sessions: 'users/sessions'}
Added after users
.
ruby:views/devise/sessions/new.html.erb
<%= form_with model: @user, url: user_session_path, class: 'sessions-main', local: true do |f| %>
<%= f.text_field :nickname, class:"nickname", id:"nickname", placeholder:"nickname" %>
<%= f.password_field :password, class:"input-default", id:"password", placeholder:"6 or more single-byte alphanumeric characters" %>
<%= f.submit "log in", class:"reg-btn" %>
<% end %>
At the very least, it looks like this ...
Similarly, it is possible to modify the newly registered controller. I wonder if I can replace the sessions part with registrations ...
Recommended Posts