Friday, January 27, 2017

How to display the selected date in the edit form in Rails?

Use the selected: option in date_select to specify what date must be displayed in the edit form.

<%= f.date_select :published_at, order: [:month, :day, :year], start_year: Date.current.year - 5, end_year: Date.current.year + 1, selected: @episode.published_at %>

If you don't use this, when you edit the record next time, you will inadvertently change the date when you change some other field.