使用rails_admin添加日期字段的默认值 [英] Add a default value for date field with rails_admin
问题描述
使用 rails(4.2.10)
和 rails_admin(1.2.0)
我正在尝试将默认日期值添加到 User
模型的 confirmed_at
字段中。
Using rails (4.2.10)
and rails_admin (1.2.0)
I am trying to addd a default date value to the confirmed_at
field of the User
Model.
它适用于编辑视图,但不适用于新视图。
It works on the edit view but not on the new view.
这是我的代码:
config.model 'User' do
list do
[...]
end
show do
[...]
end
edit do
field :confirmed_at do
default_value DateTime.now
end
exclude_fields
[...]
end
end
推荐答案
尝试返回一个字符串,并使用admin用于日期时间字段的相同格式对其进行格式化。我相信它是
Try returning a string instead and format it using the same format rails admin uses for datetime fields. I believe it to be
default_value I18n.l(DateTime.now, format: :long)
您也可以尝试使用模型上的默认值初始化对象,如下所示:
You can also try initializing the object with a default value on your model like this:
after_initialize do
self.confirmed_at = DateTime.now
end
在一个不相关的主题中,您实际上应该使用Time.zone.now以避免时区问题。
In an unrelated topic you should really be using Time.zone.now to avoid problems with time zones.
这篇关于使用rails_admin添加日期字段的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!