Rails如何验证时间和日期

时间:2022-12-09 19:48:46

Hi Im stil trying to do a little Restaurant-Website in Ruby on Rails(v. 3.2.13).

嗨我正试图在Ruby on Rails(v.3.2.13)做一个小餐馆网站。

Here you can see my current configuration: How to use params in another action?

在这里你可以看到我当前的配置:如何在另一个动作中使用params?

Right now it is possible to book a table, but there is no validation. I think i have to validate the start-time, the end-time and the date in the form_tag like the following:

现在可以预订一张桌子,但没有验证。我想我必须在form_tag中验证开始时间,结束时间和日期,如下所示:

  • Start-time has to be in the future
  • 开始时间必须在未来

  • A new reservation cant be done on the same day between Start- and End-time
  • 新的预订不能在开始和结束时间的同一天完成

  • End-time is fixed to Start-time + 2 hours
  • 结束时间固定为开始时间+ 2小时

  • The date has to be in the future or the same day
  • 日期必须是将来或同一天

Ok End-time should not be validated. Every reservation lasts 2 hours so I just have to allocate the value Start-time+2 hours.

确定不应验证结束时间。每次预订持续2小时,所以我只需要分配值Start-time + 2 hours。

I tried it with the time_selectand date_select but the format looks very odd (5i,4i,...) and I couldnt do anything with this.

我用time_select和date_select尝试了它,但格式看起来非常奇怪(5i,4i,...),我无法做任何事情。

Should I use gem for validation? Is it possible to sum times(Start-time +2 hours)? Can I use another format for time/date? Did i forget any further aspects to validate?

我应该使用gem进行验证吗?是否可以加总时间(开始时间+2小时)?我可以使用其他格式的时间/日期吗?我是否忘记了进一步验证的方面?

I dont know how to continue. Thanks

我不知道如何继续。谢谢

3 个解决方案

#1


2  

k i finally did it with Regex

我终于用Regex做了

t=params[:Starttime].scan(/\d\d/)
my_time = t[0] << ":" << t[1] << ":00"
Time.zone.parse(my_time)

this is the ref to the Time.zone.parse this will finally give you the time object that you want.
Please notify me if it worked for you.

这是对Time.zone.parse的引用,这将最终为您提供所需的时间对象。如果它适合你,请通知我。

#2


1  

Checkout validates_timeliness gem It keeps your models clean and intuitive and supports I18n localization with flexible error messages for various date/time comparisons.

Checkout validates_timeliness gem它使您的模型保持干净和直观,并支持I18n本地化和灵活的错误消息,以进行各种日期/时间比较。

validates_datetime :starts_at, :after => :now
validates_datetime :ends_at, :on_or_after => :two_days_later, :if => :starts_at

def two_days_later
  self.starts_at + 2.hours
end

#3


0  

You can change the defult format that is given by the form from which you have picked the date using this parse function and it will return you a date object in ruby. then you can apply all the validations you want very easily.

您可以使用此解析函数更改您从中选择日期的表单给出的defult格式,它将返回ruby中的日期对象。那么你可以很容易地应用你想要的所有验证。

 Date.parse(yourDate.to_s)

for checking in future there is a direct method as date_object.future? so you can check with that. and yeah it is possible to sum times in rails you can do your_time + 2.hours for adding 2 hours to the time.

为了检查将来有一个直接的方法作为date_object.future?所以你可以检查一下。是的,你可以在你的时间里加上2小时的时间,你可以用你的时间和时间来累计时间。

#1


2  

k i finally did it with Regex

我终于用Regex做了

t=params[:Starttime].scan(/\d\d/)
my_time = t[0] << ":" << t[1] << ":00"
Time.zone.parse(my_time)

this is the ref to the Time.zone.parse this will finally give you the time object that you want.
Please notify me if it worked for you.

这是对Time.zone.parse的引用,这将最终为您提供所需的时间对象。如果它适合你,请通知我。

#2


1  

Checkout validates_timeliness gem It keeps your models clean and intuitive and supports I18n localization with flexible error messages for various date/time comparisons.

Checkout validates_timeliness gem它使您的模型保持干净和直观,并支持I18n本地化和灵活的错误消息,以进行各种日期/时间比较。

validates_datetime :starts_at, :after => :now
validates_datetime :ends_at, :on_or_after => :two_days_later, :if => :starts_at

def two_days_later
  self.starts_at + 2.hours
end

#3


0  

You can change the defult format that is given by the form from which you have picked the date using this parse function and it will return you a date object in ruby. then you can apply all the validations you want very easily.

您可以使用此解析函数更改您从中选择日期的表单给出的defult格式,它将返回ruby中的日期对象。那么你可以很容易地应用你想要的所有验证。

 Date.parse(yourDate.to_s)

for checking in future there is a direct method as date_object.future? so you can check with that. and yeah it is possible to sum times in rails you can do your_time + 2.hours for adding 2 hours to the time.

为了检查将来有一个直接的方法作为date_object.future?所以你可以检查一下。是的,你可以在你的时间里加上2小时的时间,你可以用你的时间和时间来累计时间。