I am trying to have a default value for datetime_field_tag
in my view:
=form_tag form_path, method: :get do
=label_tag :sign_up_at, 'Sign up before:'
=date_field_tag :sign_up_at, (params[:sign_up_at] ||
Time.zone.today.strftime("%Y-%m-%d"))
=label_tag :last_seen_at, 'Last seen after:'
=datetime_field_tag :last_seen_at, (params[:last_seen_at] ||
Time.zone.now.strftime("%d-%m-%YT%H:%M"))
=submit_tag 'Filter', class: 'btn-info'
This results in having a value
tag in HTML:
<input type="datetime-local" name="last_seen_at" id="last_seen_at" value="22-05-2017T16:34">
My guess is, I am having some kind of a problem with parsing Time
. Can anyone help, please? Thanks!
Updated according to @deephak's answer.
You are using today
which will give you just the date
not time.
So, when you try to fetch time out of date rails will return 00:00
Example:
Time.zone.today
#=> Mon, 22 May 2017
Time.zone.now
#=> Mon, 22 May 2017 09:34:57 EDT -04:00
To fix this, change
Time.zone.today.strftime("%d-%m-%YT%H:%M")
#=> "22-05-2017T00:00"
to
Time.zone.now.strftime("%d-%m-%YT%H:%M")
#=> "22-05-2017T09:33"
# OR
Time.current.strftime("%d-%m-%YT%H:%M")
#=> "22-05-2017T09:33"