Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用activerecord在RubyonRails上验证“事件”的日期_Ruby On Rails_Ruby_Ruby On Rails 4_Activerecord - Fatal编程技术网

Ruby on rails 使用activerecord在RubyonRails上验证“事件”的日期

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

如何验证日期,以便只能显示/创建当前或未来的事件,而不是过去的事件?我正在使用activerecord,此代码可能位于事件的show.html.erb文件夹中,也可能位于控制器中,用于创建/更新这样的使用

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

你在时区工作吗?而且它只是日期还是日期时间?我不知道。只有日期。