Validation settings for Ruby on Rails login function

Set validation in name and email columns of User model

I refer to the Rails guide and so on. https://railsguides.jp/active_record_validations.html

【Thing you want to do】 ・ Leave your name and email address blank so that you cannot register. ・ Character limit for name and email address

The point is ① "presence: true" → Prohibit registration in the blank ② "length: {maximum:}" → Prohibit length longer than the number set after ":"

You will be working on the app / models / user.rb file. I created the following code.

class User < ApplicationRecord
  validates :name,  presence: true, length: { maximum: 20 }
  validates :email, presence: true, length: { maximum: 300 },
end

Check the Rails console to see if it works. First of all, when I try to register in the blank. .. ..

> User.create(name: "", email: "")
   (0.3ms)  BEGIN
   (0.4ms)  ROLLBACK
 => #<User id: nil, name: "", email: "", created_at: nil, updated_at: nil>

It is not registered by ROLLBACK.

And if you try to register a long name or email address, you can see that this is also ROLLBACK (the part marked with ... in the email column is too long to be displayed in the article).

> user = User.create(name: "a"*25 , email: "b"*350 + "@test.com")
>  (0.3ms)  BEGIN
>  (0.4ms)  ROLLBACK
=> #<User:
     id: nil,
     name: "aaaaaaaaaaaaaaaaaaaaaaaaa",
     email:"[email protected]",
     created_at: nil,
     updated_at: nil>

When displaying the reason for the error. .. ..

> user.errors.messages
> {:name=>["is too long (maximum is 20 characters)"],
 :email=>["is too long (maximum is 300 characters)"]}

I have confirmed that "length: {maximum:}" is working properly.

By the way, the part that is maximum seems to be applicable in various ways. (From the Rails guide below)

class Person < ApplicationRecord
  validates :name, length: { minimum: 2 }
  validates :bio, length: { maximum: 500 }
  validates :password, length: { in: 6..20 }
  validates :registration_number, length: { is: 6 }
end

:minimum:The attribute cannot take a value smaller than this value.
:maximum:The attribute cannot take a value greater than this value.
:in or:within:The length of the attribute must be within the given interval. The value of this option must be a range.
:is:The length of the attribute must be equal to the given value.

Recommended Posts

Validation settings for Ruby on Rails login function
Implementation of Ruby on Rails login function (Session)
Ruby on Rails validation summary
Implementation of Ruby on Rails login function (devise edition)
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
[Ruby on Rails] Introduced paging function
[Ruby on Rails] CSV output function
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Ruby on Rails] Implement login function by add_token_to_users with API
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] About bundler (for beginners)
[Ruby on Rails] Follow function implementation: Bidirectional
(2021) Ruby on Rails administrator (admin) login creation
Explanation of Ruby on rails for beginners ①
[Ruby on rails] Implementation of like function
[Ruby on Rails] Logical deletion (withdrawal function)
Ruby on Rails for beginners! !! Post list / detailed display function summary
[Ruby on Rails] Select2 introduction memo for Webpacker
Ruby on Rails Elementary
Ruby on Rails basics
Ruby on Rails Email automatic sending function implementation
[Rails] Procedure for linking databases with Ruby On Rails
Ruby On Rails Association
[Ruby on Rails] Post editing function (update, delete)
[Ruby on Rails] Asynchronous communication of posting function, ajax
[Ruby on Rails] Posting score ranking function (whole display)
[Ruby on Rails] Implementation of tagging function/tag filtering function
[Ruby on Rails] Post image preview function in refile
Challenge the settings for developing with vue.js on Rails 6
Explanation of Ruby on rails for beginners ② ~ Creating links ~
[Ruby on Rails] Search function (model, method selection formula)
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Commentary on partial! --Ruby on Rails
Login function implementation with rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails Basic Memorandum
[Ruby on Rails] How to implement tagging / incremental search function for posts (without gem)
[Rails] Settings for loading web fonts in CloudFront on Heroku
Definitely useful! Debug code for development in Ruby on Rails
Ruby on Rails Email automatic sending function setting (using gmail)
[Ruby on Rails] Bookmark (favorite registration, like) function: One direction
Ruby on Rails for beginners! !! Summary of new posting functions
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
A note about the seed function of Ruby on Rails
[Apple login] Sign in with Apple implementation procedure (Ruby on Rails)
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Ruby on Rails Overview (Beginner Summary)