在Rails的独立的日期和时间表单字段 [英] Separate date and time form fields in Rails

查看:149
本文介绍了在Rails的独立的日期和时间表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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 ),和属性装饰(<一href="https://rails.lighthouseapp.com/projects/8994/tickets/950-attributedecorator-a-new-take-on-aggregation">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

这篇关于在Rails的独立的日期和时间表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆