Add a default value for date field with rails_admi

2019-07-07 19:48发布

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条回答
Explosion°爆炸
2楼-- · 2019-07-07 20:11

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.

查看更多
登录 后发表回答