ruby (2.6.5) rails(6.0.0) devise (4.7.2)
First, specify the link destination so that the routed users / registrations # edit will be executed.
Prefix Verb URI Pattern Controller#Action
edit_user_registration GET /users/edit(.:format) users/registrations#edit
As described below.
<%= link_to 'My page', edit_user_registration_path(current_user), class: "user-nickname" %>
Since the function to update data without a password is implemented in devise's controller, Generate a devise controller in the terminal.
$ rails g devise:controllers users
Edit the generated RegistrationsController as follows.
app>controllers>users>registrations_controlle.rb
class Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_account_update_params, only: [:update]
protected
def update_resource(resource, params)
resource.update_without_password(params)
end
def after_update_path_for(_resource)
root_path
end
def configure_account_update_params
devise_parameter_sanitizer.permit(:account_update, keys: [:nickname])
end
end
I am updating with update_resource without a password. After_update_path_for specifies the redirect destination after updating. In configure_account_update_params, this time only the column called nickname of the User table is allowed to be updated.
See the official wiki for details. https://github.com/heartcombo/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password
Generate a devise view file in the terminal.
$ rails g devise:views
Edit edit.html.view of the generated view file so that it has only the required input form.
erb:app>views>devise>registrations>edit.html.erb
<h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>
<div class="field">
<%= f.label :nickname %><br />
<%= f.text_field :nickname, autofocus: true, autocomplete: "nickname" %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
Edit as follows and specify the controller at the time of registration.
routes.rb
devise_for :users, controllers: {
registrations: 'users/registrations'
}
When updating, be careful not to be hit by password validation It is added as on :: create. With this description, password validation will be applied only when the create action is executed.
app>models>user.rb
with_options presence: true do
validates :nickname, :birthday
validates :email, uniqueness: true
validates :first_name, :last_name, format: { with: regexp_name }
validates :first_name_read, :last_name_read, format: { with: regexp_name_read }
validates :password, format: { with: regexp_password }, on: :create
end
By implementing the above, it was possible to update the table of devise's User model without a password. Thank you for visiting our website.
Recommended Posts