如何在 Rails ActiveAdmin 表单的 Ruby 中格式化日期时间字段?
How to format datetime field in Ruby on Rails ActiveAdmin form?
我正在使用 Ruby 2.1 和 Rails 4.1。我安装了 ActiveAdmin (1.0.0.pre2)。我想在 ActiveAdmin 表单中格式化日期时间字段 expires_at
。我在 /app/admin/job.rb
中尝试使用 jQuery 日期选择器选项:
f.input :expires_at, as: :datepicker, datepicker_options: { date_format: "yy-mm-dd", min_date: Time.to_s + "+7D" }
在新模式下完美运行/admin/jobs/new
,但在编辑模式下不运行/admin/jobs/xx/edit
。它总是显示来自数据库的值,例如 2015-11-06 15:10:00 UTC
.
我也试过 :value
,但它也不起作用。
f.input :expires_at, :value => :expires_at.try(:strftime, '%Y-%m-%d'), as: :datepicker, datepicker_options: { min_date: Time.to_s + "+7D" }
我在/config/locales/en.yml
中也有如下配置。但是,我相信它不会影响此类表单日期时间字段。
en:
date:
formats:
long: "%Y-%m-%d"
time:
formats:
long: "%Y-%m-%d %H:%M"
根据 this SO question,value 属性必须在 formtastic 中用 input_html
包装。
f.input :expires_at, :input_html => { :value => f.object.expires_at.try(:strftime, '%Y-%m-%d') }
我正在使用 Ruby 2.1 和 Rails 4.1。我安装了 ActiveAdmin (1.0.0.pre2)。我想在 ActiveAdmin 表单中格式化日期时间字段 expires_at
。我在 /app/admin/job.rb
中尝试使用 jQuery 日期选择器选项:
f.input :expires_at, as: :datepicker, datepicker_options: { date_format: "yy-mm-dd", min_date: Time.to_s + "+7D" }
在新模式下完美运行/admin/jobs/new
,但在编辑模式下不运行/admin/jobs/xx/edit
。它总是显示来自数据库的值,例如 2015-11-06 15:10:00 UTC
.
我也试过 :value
,但它也不起作用。
f.input :expires_at, :value => :expires_at.try(:strftime, '%Y-%m-%d'), as: :datepicker, datepicker_options: { min_date: Time.to_s + "+7D" }
我在/config/locales/en.yml
中也有如下配置。但是,我相信它不会影响此类表单日期时间字段。
en:
date:
formats:
long: "%Y-%m-%d"
time:
formats:
long: "%Y-%m-%d %H:%M"
根据 this SO question,value 属性必须在 formtastic 中用 input_html
包装。
f.input :expires_at, :input_html => { :value => f.object.expires_at.try(:strftime, '%Y-%m-%d') }