【问题标题】:Rails time comparisonRails 时间比较
【发布时间】:2013-08-18 14:57:53
【问题描述】:

我正在尝试验证来自 from 的一些用户输入。特别是我试图验证来自date_selecttime_select 的输入。我处理输入如下:

View的输入标签:

<%= date_select :date, 'date', use_short_month: true %>
<%= time_select :Starttime , 'Starttime', default: Time.now, minute_step: 30,:ignore_date => true %>

将输入转换为时间对象(在控制器中):

@Start = Time.new(date['date(1i)'].to_i, date['date(2i)'].to_i, date['date(3i)'].to_i, t_start["Starttime(4i)"].to_i, t_start["Starttime(5i)"].to_i)

如果我在视图中显示@Start 一切正常! 2013-08-18 16:00:00 +0200

现在我尝试检查有效输入的时间/日期。因此我在控制器中做了这个功能:

def check_date
if @Start < Time.now
  flash[:notice ]= "Booking has to be in the future!"
  return false
else
  return true
end
end

但它不起作用。 err_msg。 undefined method '&lt;' for nil:NilClass 出现。我不知道为什么。 Time.now@Start 的视图输出是相同的。

我也尝试过使用 DateTime-object 而不是 Time-object,但出现了同样的错误。有什么想法吗?

编辑:

索引操作:

def index
t_start= params[:Starttime]
date= params[:date]
if t_start && date
  @Start = Time.new(date['date(1i)'].to_i, date['date(2i)'].to_i, date['date(3i)'].to_i, t_start["Starttime(4i)"].to_i, t_start["Starttime(5i)"].to_i)
end
check_date
@tables = Table.search(params[:search])
end

谢谢

【问题讨论】:

  • 你是如何调用这个函数的?
  • 在索引操作中使用check_date
  • 也粘贴有问题的索引方法
  • 您需要发布更多代码,以便我们看到交互。
  • @RailsGuy 我添加了索引方法。还要别的吗。有人有想法吗?

标签: ruby-on-rails validation date time comparison


【解决方案1】:

我自己发现了错误...方法调用必须在 if 查询中,否则 @Start 没有价值。

谢谢

【讨论】:

    猜你喜欢
    • 2017-10-21
    • 2018-05-29
    • 2016-12-01
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 2019-04-07
    • 1970-01-01
    相关资源
    最近更新 更多