0

这就是前端当前日期自动保存在数据库中的方式(没有 datepickers n 东西):

<% for order in @orders %>
<%= order.updated_at.strftime("%Y-%m-%d %I:%M") %>

有效,数据库中的数据:2013-05-03 21:32:59.847009

但问题是,对于后端,我使用的是 Active Admin,并且我正在制作表单,因此可以编辑订单。但问题是我真的不明白如何在那里应用相同的 strftime 函数,我认为它会是这样的:

f.input :updated_at, :date.strftime("%Y-%m-%d %I:%M")

但这是不正确的。得到错误:

undefined method `strftime' for :date:Symbol

有谁知道如何在该行中正确实施 strftime 的解决方案?

4

3 回答 3

1

尝试这个:

f.input :updated_at, :value => order.updated_at.try(:strftime,'%Y-%m-%d %I:%M')

try当日期为零时,将阻止您获得异常。

于 2013-05-04T08:25:25.620 回答
0

If I got your question:

t.timestamps in table migration, creates these created_at and updated_at field additionally. These fields are default datetime typed field. And updated automatically when a record created or updated.

But if you want to add a field of datetime typed, define column type as :datetime like:

run rails g migration add_time_to_table

this command creates a migration file, edit this file like:

class AddTimeToTable < ActiveRecord::Migration
  def self.up
    add_column :table, :time, :datetime
  end

  def self.down
    remove_column :table, :time
  end
end

Now this Table model contains a time attribute. So in your ActiveAdmin view you can input time as datetime format

like:

f.input :time
于 2013-05-04T07:57:30.067 回答
0

Active Admin 使用 config/locales 中的 yaml 文件来确定日期的格式。

如果您希望默认日期为其他日期(假设为英语,则在 config/locales/en.yml 中):

en:
  date:
    formats:
      long: "%Y-%m-%d %I:%M"
于 2013-05-03T23:24:59.710 回答