Search code examples
ruby-on-railsdateruby-on-rails-4simple-form

Simple form date_field not populating for edit


I'm using rails 4 simple form to store some data. My date field stores fine, but the form input is mm-dd-yyyy and stores with yyyy-mm-dd.

When I do an edit action on the form, everything returns except for the date, which doesn't fill in.

view

<div class="input-group">
 <%= f.date_field :date_requested, as: :date%>
</div>

model

attribute :date_requested, :datetime

date field

enter image description here


Solution

  • It is because of the format of data stored in the database and the format of the date you are showing on view. Database stores date in yyyy/mm/dd and it seems like on view you want to show in mm/dd/yyyy format

    <%= f.date_field :date_requested, as: :date, value: f.object.date_requested.try(:strftime,"%m/%d/%Y") %>
    

    Hope this helps.