Add a default value for date field with rails_admin

╄→尐↘猪︶ㄣ 提交于 2019-12-11 00:19:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!