我在 Rails 应用程序中使用 Chronic,如下所示。我有一个表单,其中包含日期和时间的单独文本字段(jquery 下拉列表)。在我的模型中,我有虚拟属性,应该使用 Chronic 将组合数据时间写入数据库进行解析。Chronic 正在正确解析日期,但无论我在文本字段中输入什么时间,它都会在 datetime 字段中输出 12:00:00 作为时间。我已经测试了 IRB 中的输入类型,它似乎工作正常,我在这里遗漏了什么吗?
模型:
class VisitBlock < ActiveRecord::Base
attr_accessible :start_date_text, :time_text
attr_writer :start_date_text, :time_text
before_save :save_start_date_text
def time_text
end
def start_date_text
@start_date_text || visit_date_start.try(:strftime, "%m-%d-%Y")
end
def save_start_date_text
self.visit_date_start = Chronic.parse("#{start_date_text} #{time_text}").in_time_zone if @start_date_text.present?
end
控制台日志:
Started POST "/visit_blocks" for ************** at 2013-04-21 19:56:20 +0000
Processing by VisitBlocksController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ns4YyAL9a44RU3T/wOw+DPcigVbBd6eQ5wz1exq3g1M=", "visit_block"=>{"start_date_text"=>"04/22/2013", "time_text"=>"05:15 AM"}, "hou
r"=>"05", "minute"=>"15", "meridian"=>"AM", "button"=>""}
Client Load (4.8ms) SELECT "clients".* FROM "clients" WHERE "clients"."last_name" IS NULL LIMIT 1
(0.1ms) begin transaction
SQL (3.2ms) INSERT INTO "visit_blocks" ("visit_date_start") VALUES (?) [["created_at", Sun, 21 Apr 2013 19:56:20 UTC +00:00], ["updated_at", Sun, 21 Apr 2013 19:56:20 UTC +00:00], ["visit_date_start", Mon, 22 Apr 2013 12:00:00 UTC +00:00]]
(6.7ms) commit transaction