[RAILS] 6 points to doubt when user registration is not possible with devise

How far did you go

--Install devise --Create a model --Add column to migration file --Created a form with form_with by putting the column names of the users table in f.label and f.text_field.

This is a checklist when the form is not saved in the database even if it is filled in in this state. I will omit the details of what columns exist in the users table.

1. Validation

Let's see if the model has strange constraints.

user.rb


  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
         validates :name, presence: true
         validates :profile, presence: true
         validates :occupation, presence: true
         validates :position, presence: true

2. Migration file constraints

Let's see if there are any strange restrictions when creating the table.

devise_create_users.rb


      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""
      t.string :name
      t.text :profile
      t.text :occupation
      t.text :position

3. Strong parameters

devise doesn't have strong parameters, so we'll use devise_parameter_sanitizer instead. I was missing one column. Let's review. Specifically, I forgot to allow the email column, but when I ran it in the terminal, I noticed that something was suspicious because it was ROLLBACKed after the email as shown in this image. ..

image.png

application_controller.rb


class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  private
  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [ :email, :encrypted_password, :name, :profile, :occupation, :position])
  end
end

4. Association

It doesn't matter if only one table is created, but if there are multiple tables, has_many plural, longs_to singular, etc.

5. password column name

In the end this was the cause. It is a column (encryped_password by default) of the users table in devise, but when you enter the password and password of form_with, you have to describe the column name as password and password_confimation respectively. This is because devise implements the ability to check if password and password_confimation are the same, and if they are the same, return an encrypted string to encrypted_password. image.png

mistake

        <%= form_with model: @user, url: user_registration_path, local: true do |f| %>

        <div class="field">
          <%= f.label :encryped_password, "Password (6 characters or more)" %><br />
          <%= f.password_field :encryped_password, autocomplete: "new-password" %>
        </div>

        <div class="field">
          <%= f.label :encryped_password, "Re-enter password" %><br />
          <%= f.password_field :encryped_password, autocomplete: "new-password" %>
        </div>
The following is omitted

Correct answer

        <%= form_with model: @user, url: user_registration_path, local: true do |f| %>

        <div class="field">
          <%= f.label :password, "Password (6 characters or more)" %><br />
          <%= f.password_field :password, autocomplete: "new-password" %>
        </div>

        <div class="field">
          <%= f.label :password_confirmation, "Re-enter password" %><br />
          <%= f.password_field :password_confirmation, autocomplete: "new-password" %>
        </div>
The following is omitted

6. Caused by database visualization software

It happened somewhere else before, but it's possible that the software that visualizes the database (Sequel Pro in my case) was actually saved after restarting, so please check.

that's all

Recommended Posts

6 points to doubt when user registration is not possible with devise
devise user registration
Points to review when Rubocop is slow to run
[For beginners] Test devise user registration with RSpec
Hot deploy with STS (Eclipse) is not possible
What to do when is invalid because it does not start with a'-'
Transit to confirmation page when logging out with devise
Suddenly bundle install is not possible due to gem'ffi'
[Ruby + Rails] When you want to register in Mailchimp's mail list together with user registration
When changing user information using devise Settings on the edit screen when the password is not saved
Edit user information with Devise
What to do if the app is not created with the latest Rails version installed when rails new
When requested access to the resource is denied when pushing with Docker
Is it possible to automatically generate Getters / Setters with Java Interface?
[Java] Points to note with Arrays.asList ()
[IOS] What to do when the image is filled with one color
[Rails] How to get the user information currently logged in with devise
Notes on what to do when EC2 is set up with t2.micro
When I try to sign up with devise, it automatically redirects to root_path
[Rails] devise customization. How to change the redirect page after user registration and editing, and how to skip password input when editing