4
5

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 5 years have passed since last update.

rails 日付のvalidation

Posted at

######備忘録
予約機能の開始時間のvalidationについて
過去の日付を選べないようにするために
-で囲まれた部分を追加

Reservation.rb
class Reservation < ApplicationRecord
  belongs_to :user, optional: true
  belongs_to :menu, optional: true
  validates :start_date, presence: true

--------------------------------
  validate :date_before_start


  def date_before_start
    errors.add(:start_date, "は今日以降のものを選択してください") if
    start_date < Date.today
  end
--------------------------------
end

参考にしたサイト
Ruby on Railsで、日付の前後入力チェック

4
5
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
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?