Ruby on rails 使用activerecord在RubyonRails上验证“事件”的日期
如何验证日期,以便只能显示/创建当前或未来的事件,而不是过去的事件?我正在使用activerecord,此代码可能位于事件的show.html.erb文件夹中,也可能位于控制器中,用于创建/更新这样的使用Ruby on rails 使用activerecord在RubyonRails上验证“事件”的日期,ruby-on-rails,ruby,ruby-on-rails-4,activerecord,Ruby On Rails,Ruby,Ruby On Rails 4,Activerecord,如何验证日期,以便只能显示/创建当前或未来的事件,而不是过去的事件?我正在使用activerecord,此代码可能位于事件的show.html.erb文件夹中,也可能位于控制器中,用于创建/更新这样的使用 validates :date, date: { after: Proc.new { Date.today } } #, on: :create # if you want only to validate on creation def EventController
validates :date,
date: { after: Proc.new { Date.today } } #, on: :create # if you want only to validate on creation
def EventController < ApplicationController
before_action :set_event
before_action :check_date
...
private
def check_date
redirect_to :back, notice: 'This event has ended' if @event.date < Date.today
end
end
对于show,您可以在控制器中使用这样的before_动作
validates :date,
date: { after: Proc.new { Date.today } } #, on: :create # if you want only to validate on creation
def EventController < ApplicationController
before_action :set_event
before_action :check_date
...
private
def check_date
redirect_to :back, notice: 'This event has ended' if @event.date < Date.today
end
end
你在时区工作吗?而且它只是日期还是日期时间?我不知道。只有日期。