1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rails6 バリデーションをかける

Posted at

目的

  • 空欄でのDB登録を避けるためのバリデーションの記載方法をまとめる

空での保存を避けるバリデーション

  • 下記の記載を当該のモデルファイルに記載する。

    class Post < ApplicationRecord
      validates :バリデーションをかけたいカラム名, {presence: true}
    end
    

空での保存を防ぎ、かつ総文字数を140文字以下にするバリデーション

  • 下記の記載を当該のモデルファイルに記載する。

    class Post < ApplicationRecord
        validates :content, {presence: true, length: {maximum: 140}}    
    end
    
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?