I'm making a scheduling app for my small photo business.
I started with this in my SimpleForm:
<%= f.input :starts_at, :as => :string %>
and this in my coffeescript:
dateFormat: 'yy-mm-dd',
timeFormat: 'hh:mm tt'
This works fine to create a new record.
However, if I go to edit that record, the time part of the datetime picker doesn't display the previously set time, displaying instead the plugin's default.
Trent suggested this was because the time date field in the existing record needs to match what the jQuery module expects to see in terms of formatting.
I modified the SimpleForm input as follows:
<%= f.input :starts_at, :as => :string, :input_html => { :value => localize(f.object.starts_at, :format => "%m-%d-%Y %I:%M %P") } %>
This works perfectly for an existing record but throws an error when creating a new record since there is no existing :value to parse.
I tried the following (and variations of the following) with no success:
<%= f.input :starts_at, :as => :string, :input_html => { :value => localize(f.object.starts_at, :format => "%m-%d-%Y %I:%M %P") if :value.present? } %>
I next tried creating a custom SimpleForm input, apparently stumbling across the same
source Thomas did, but, similar to Thomas' experience, couldn't manage to have this format the text input field, even after putting the following in the en.yml and simple_form.en.ymf locales files:
datetime:
formats:
default: ! '%Y-%m-%d %I:%M %P'