Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format datetime field in Ruby on Rails ActiveAdmin form?

I'm using Ruby 2.1 and Rails 4.1. I installed ActiveAdmin (1.0.0.pre2). I want to format a datetime field expires_at in an ActiveAdmin form. I tried this in /app/admin/job.rb using jQuery datepicker option:

f.input :expires_at, as: :datepicker, datepicker_options: { date_format: "yy-mm-dd", min_date: Time.to_s + "+7D" }

It perfectly works in the form of new mode /admin/jobs/new, but it does not work in the edit mode /admin/jobs/xx/edit. It always shows the value from db such as 2015-11-06 15:10:00 UTC.

I also tried with :value, but it does not work either.

f.input :expires_at, :value => :expires_at.try(:strftime, '%Y-%m-%d'), as: :datepicker, datepicker_options: { min_date: Time.to_s + "+7D" }

I also have the following configuration in /config/locales/en.yml. However, I believe that it does not affect such form datetime fields.

en:
  date:
    formats:
      long: "%Y-%m-%d"
  time:
    formats:
      long: "%Y-%m-%d %H:%M"
like image 685
Sithu Avatar asked Oct 11 '15 12:10

Sithu


1 Answers

According to this SO question, the value attribute have to be wrapped with input_html in formtastic.

f.input :expires_at, :input_html => { :value => f.object.expires_at.try(:strftime, '%Y-%m-%d') }
like image 97
nayiaw Avatar answered Sep 29 '22 03:09

nayiaw