0

I would need to implement jQuery datetimepicker into my Ruby on Rails 5 application. There is good documentation for jqueryui Date picker, included two railscasts by Ryan Bates.
As datetimepicker is concerned, I suppose the only concern about rails is to produce a date and time format that rails can easily understand. Once this issue is solved, I think it is easy to add the plugin to the asset pipeline.

Rails DateTime format is "yyyy-mm-dd hh:mm:ss". For instance "2017-07-28 11:24:53".
So I wonder: is there any way to change the default datetimepicker format into a DateTime rails format ?

In the datetimepicker official webpage, the section dedicated to using another date parser/formatter uses as example the MomentJS library. However I could not find examples useful for Rails applications.

jQuery UI Datepicker has a dateFormat option which can be easily used to set the date format to the desired format: how can this be done with datetimepicker?

Asarluhi
  • 1,280
  • 3
  • 22
  • 43
  • 2
    have you tried the to_datetime method ? – Snake Jul 28 '17 at 09:26
  • method `to_datetime` should be used to convert `params[:event][:event_start]` before saving it in the database, and I would not know how to do it. – Asarluhi Jul 28 '17 at 09:36

1 Answers1

1

In your view :

= form_for @event do |f|
  = f.text_field :event_start, class: "datepicker"

In your controller

def create
  # before the save
  @event.event_start = params[:event][:event_start].to_datetime
end
Snake
  • 1,157
  • 1
  • 10
  • 21
  • Since I use `@event = current_user.events.build(event_params)` in the create action, where `event_params` is defined as `params.require(:event).permit(:user_id, :event_start, :place)`, I wonder if I can define `params[:event][:event_start] = params[:event][:event_start].to_datetime` inside `event_params` before `params.require...`. – Asarluhi Jul 28 '17 at 09:51
  • You can achieve that with the date_field form helper : https://apidock.com/rails/v4.0.2/ActionView/Helpers/FormHelper/date_field – Snake Jul 28 '17 at 10:01