[RUBY] How to update devise user information without a password


ruby (2.6.5) rails(6.0.0) devise (4.7.2)

Implementation of user information edit page

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" %>

Edit devise controller

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.


class Users::RegistrationsController < Devise::RegistrationsController
  before_action :configure_account_update_params, only: [:update]

  def update_resource(resource, params)

  def after_update_path_for(_resource)

  def configure_account_update_params
    devise_parameter_sanitizer.permit(:account_update, keys: [:nickname])

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

Edit view file of devise edit

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.


<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 class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true, autocomplete: "email" %>

  <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
    <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
  <% end %>

  <div class="actions">
    <%= f.submit "Update" %>
<% end %>

Edit routes.rb

Edit as follows and specify the controller at the time of registration.


devise_for :users, controllers: {
    registrations: 'users/registrations'

User model password validation edit

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.


  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

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

How to update devise user information without a password
How to update user edits in Rails Devise without entering a password
It was surprisingly easy. How to update user information without entering a password
[Rails] How to get the user information currently logged in with devise
Implement user edit / update function without using devise
How to create a class that inherits class information
How to make a cache without thinking too much
[Ruby/Rails] How to generate a password in a regular expression
[Rails] How to install devise
How to leave a comment
How to update with activerecord-import
How to insert a video
How to create a method
Edit user information with Devise
[iOS] [Objective-C] How to update a widget from an Objective-C app
How to output array values without using a for statement
How to join a table without using DBFlute and sql
How to give Spring Security authenticated user information other than userId and password and how to refer to it
[Rails] How to use gem "devise"
How to add columns to a table
[Rails] How to use devise (Note)
[rails] How to display db information
How to create a registration / update function where the table crosses
[Java] How to update Java on Windows
How to make a Java container
How to sign a Minecraft MOD
How to make a JDBC driver
[Java] How to create a folder
How to write a ternary operator
[Rails] [Devise] Edit profile without password
[Swift] How to send a notification
How to make a splash screen
How to make a Jenkins plugin
How to store the information entered in textarea in a variable in the method
How to make a Maven project
How to make a Java array
[Rails 5] How to display the password change screen when using devise
[Rails] How to log in with a name by adding a devise name column
How to display error messages and success messages when registering as a user
[Rails] Use devise to get information about the currently logged in user
[Rails] How to pass validation such as password when executing update action
[Xcode] How to add a README.md file
How to execute a contract using web3j
How to make a Java calendar Summary
A memorandum on how to use Eclipse
How to redo a deployment on Heroku
[Basic] How to write a Dockerfile Self-learning ②
How to insert a video in Rails
How to add a new hash / array
[Introduction to Java] How to write a Java program
How to create a Maven repository for 2020
Creating a user authentication function using devise
How to make a Discord bot (Java)
How to update related models with accepts_nested_attributes_for
How to print a Java Word document
[Swift5] How to create a splash screen
[rails] How to create a partial template
How to handle sign-in errors with devise
[Note] How to use Rails 6 Devise + cancancan
How to publish a library in jCenter
How to add application version information to Sentry information