[Rails] How to use helper method, confimartion

At first

I often see services that are designed to enter the password twice for confirmation when entering the password. When I tried to implement the same thing, I could easily implement it by using the helper method of validation in Rails called confimartion, so I will leave it as a memorandum.

What is conn fimartion?

Use this when you need to get an exact match for what you receive in two text fields. For example, suppose you use a confirmation field in your email address or password. This validation helper creates virtual attributes. The name of the attribute will be the attribute name you want to check with "_confirmation" added. https://railsguides.jp/active_record_validations.html Quoted from above.

In short, I think there is no problem in recognizing that it is a method that applies validation that can only be registered with the same data in the two forms.

Implementation procedure

1. Model modification

class User < ApplicationRecord
  validates :password, confirmation: true
end

Add the above description to the column to which you want to apply the confimartion method to the model. This completes validation.

2. Add confirmation form to view

 = form_for(@user, url: user_registration_path) do |f|  
  = f.text_field :password 
  = f.text_field :password_confirmation
end

For the attribute name of the second argument of the text field, add _confimartion to the attribute name you want to check with confimartion. The model and view actually described are as follows. スクリーンショット 2020-05-29 12.05.37.png

スクリーンショット 2020-05-29 12.03.40.png

If the values entered in the two forms are not the same, it will be validated! If you want to add validation, just add validation to the attribute you want to check.

When first implemented ・ Create an attribute for confirmation -Create a conditional branch to check if the values entered in the two forms are the same ・ If they are the same, save I thought I had to write code like this, so using confirmation made the implementation very easy.

References

https://railsguides.jp/active_record_validations.html

Recommended Posts

[Rails] How to use helper method, confimartion
[rails] How to use devise helper method before_action: authenticate_user!
[Rails] How to use the map method
[Rails] How to use enum
[Rails] How to use enum
How to use rails join
[Rails] How to use validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Ruby on Rails] How to use session method
[Rails] How to use Scope
How to use the link_to method
[Rails] How to use gem "devise"
How to use the include? method
[Rails] How to use devise (Note)
[Rails] How to use flash messages
[Java] How to use join method
How to use Ruby on Rails
[Ruby] How to use any? Method
[Rails] How to use Active Storage
[Introduction to Rails] How to use render
How to use Ruby inject method
How to use custom helpers in rails
[Ruby on Rails] How to use CarrierWave
[Rails] How to use rails console with docker
How to use submit method (Java Silver)
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Java] How to use the toString () method
How to use MySQL in Rails tutorial
[Ruby on Rails] How to use redirect_to
[Note] How to use Rails 6 Devise + cancancan
[Ruby on Rails] How to use kaminari
[Rails] How to use video_tag to display videos
How to use credentials.yml.enc introduced in Rails 5.2
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
How to uninstall Rails
[How to use label]
How to use identity
How to use hashes
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Rails] How to use select boxes in Ransack
How to use rails g scaffold, functions, precautions
[Ruby] How to use gsub method and sub method
Output of how to use the slice method
How to use the replace () method (Java Silver)