在Rails的独立的日期和时间表单字段字段、表单、独立、日期

由网友(玍玍玍╮不弃)分享简介:我有一个ActiveRecord模型事件用datetime列 starts_at 。我想present一种形式,其中的日期和时间 starts_at 单独选择(如23-10-2010日期和18:00的时间)。这些字段应该由单个列 starts_at 进行备份,并且验证应该preferably反对 starts_at ,...

我有一个ActiveRecord模型事件用datetime列 starts_at 。我想present一种形式,其中的日期和时间 starts_at 单独选择(如23-10-2010日期和18:00的时间)。这些字段应该由单个列 starts_at 进行备份,并且验证应该preferably反对 starts_at ,太。

I have an ActiveRecord model Eventwith a datetime column starts_at. I would like to present a form, where date and time for starts_at are chosen separately (e.g. "23-10-2010" for date and "18:00" for time). These fields should be backed by the single column starts_at, and validations should preferably be against starts_at, too.

我当然可以渣土周围的虚拟属性和挂钩,但我想一个更好的解决方案。我已经尝试了两种 composed_of ( RDOC ),和属性装饰(lighthouse讨论, GitHub的)没有成功。

I can of course muck around with virtual attributes and hooks, but I would like a more elegant solution. I have experimented with both composed_of (rdoc), and attribute-decorator (lighthouse discussion, github) without success.

下面是什么,我想。

class Event < ActiveRecord::Base
  validates_presence_of :start_date
end

# View
# On submission this should set start_date.
form_for @event do |f|
  f.text_field :starts_at_date         # date-part of start_date
  f.text_field :starts_at_time_of_day  # time-of-day-part of start_date
  f.submit
end

任何帮助AP preciated。

Any help appreciated.

推荐答案

在看这个今天Rails项目,和碰到这种宝石:

Was looking at this today for a Rails project, and came across this gem:

https://github.com/shekibobo/time_splitter

设置DateTime是否可以是一个困难的或丑陋的东西,特别是通过网络的形式。找到一个好的的DatePicker或TimePicker很容易,但让他们工作既可能是困难的。 TimeSplitter自动生成访问日期,时间,小时,分钟,你的日期时间或时间属性,使得它琐碎使用不同的表单输入设置日期时间字段的不同部分。

Setting DateTimes can be a difficult or ugly thing, especially through a web form. Finding a good DatePicker or TimePicker is easy, but getting them to work on both can be difficult. TimeSplitter automatically generates accessors for date, time, hour, and min on your datetime or time attributes, making it trivial to use different form inputs to set different parts of a datetime field.

看起来像它会做的工作适合你

Looks like it would do the job for you

阅读全文

相关推荐

最新文章