问题
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.
Here is my code :
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
回答1:
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
In an unrelated topic you should really be using Time.zone.now to avoid problems with time zones.
来源:https://stackoverflow.com/questions/46644974/add-a-default-value-for-date-field-with-rails-admin