If you are new to Rails and want to make your own validation, stop by this finger.

About this article

A free article written by rails beginners for rails beginners. Recently, my friend's children have been rushing to say "** shit !!!!!!!!", so validation to eliminate the sweet idea that ** shit ** would be interesting. The purpose is to make good use of and not to spread vulgar words to the world.

environment

Rails5

Advance preparation

--Run rails new --Create controllers, models and tables, and set routing appropriately.

Practice

This time, if a content containing a word (shit) that is not suitable for the content column of the Post model is about to be posted, we will issue a verification error to protect the world of love and peace of service and prevent spreading dirty words. ..

Posts table columns

ActiveRecord::Schema.define(version: 2018_06_10_125752) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "posts", force: :cascade do |t|
    t.string "title", null: false
    t.text "content", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.bigint "user_id", null: false
    t.index ["user_id"], name: "index_tasks_on_user_id"
  end
end

Now, let's add a method for validation to the Post model.

Step 1

I will register the method for verification.

class Ppst < ApplicationRecord
  validate :validate_content_not_including_unko

Hey, isn't there something strange?

Those who have studied Rails to some extent here will want to put in a tsukkomi. so! !! !! That there is no ** validate ** in the third person singular present (third person) s ** instead of ** validates **! !! !! !! The difference between the two seems to be either custom or self-made validation.

Apparently, when using custom validation, use ** validate **. .. .. Horrible and difficult to understand https://guides.rubyonrails.org/active_record_validations.html#custom-methods

Step 2

I will implement a method for verification

class Ppst < ApplicationRecord
  validate :validate_name_not_including_unko

  private

  def validate_content_not_including_unko
    errors.add(:content, 'If you write this, you can't! !! !! !!') if content.include?('shit')
  end
end

①private Since this method is not used from outside this object, it is described below private

validate_content_not_including_unko The job of the validation method is ** "If you find a validation error, put the error content in errors" **. Therefore, when content.include? ('Unko') contains shit in the content column, the content of the verification error is included in errors.add.

Check if poop spreads in the world.

スクリーンショット 2020-11-13 14.52.28.png

This will stop you from saying that bad boy. .. ..

Finally

There is one thing I thought through this article that it had nothing to do with it.

Instead of making it possible to post suddenly by typing a sentence on Twitter or pressing the post button, to take a breather on the confirmation screen once, "Are you saying or acting like being dried?" I wondered if I could reduce the number of sentences that would be emotionally written and spread some kind of adverse effect.

Also, I'm sorry I had a lot of poop.

Recommended Posts

If you are new to Rails and want to make your own validation, stop by this finger.
If you are using Android Room and want to change the column definition
Apply your own domain to Rails of VPS and make it SSL (https) (CentOS 8.2 / Nginx)
[# 3 Java] Read this if you want to study Java! ~ Carefully selected ~
Make your own Rails validate
[PostgreSQL] If you want to delete the Rails app, delete the database first!
If you want to step up Ruby → Udemy "Introduction to Ruby on Rails for the first time-Learn Ruby and Rails from the basics and publish your web application online"
If you want to make a zip file with Ruby, it's rubyzip.
Basic Rails commands you want to learn