Optional time_select with allow_blank defaults to 00:00

ruby-on-rails

Solution

Your param is not [:event][:end_time], they are:

[:event]['end_time(1i)']..[:event]['end_time(5i)'] respectively.

Making the param value nil won't help, you need to reject that key from your params hash; e.g.

` params[:event].except('end_time(4i)', 'end_time(5i)') if params[:event]['end_time(4i)'].blank? `

Problem

I'm trying to make a time field in my form optional. I'm doing this: ``` <%= f.time_select :end_time, minute_step: 5, include_blank: true %> ``` However when it is submitted, since there is default date information being submitted automatically with time_select, this is what the params looks like: `end_time(1i): '2000' end_time(2i): '1' end_time(3i): '1' end_time(4i): '' end_time(5i): ''` Rails interprets this as a real date and sets end_time to 00:00 Is there any fix/workout to be able to have an optional time field? I tried this in the controller: ``` if params[:event][:end_time] && params[:event][:end_time][:hour].blank? && params[:event][:end_time][:minute].blank? params[:event][:end_time] = nil end ``` But it doesn't seem to work. Any help is appreciated!

Original source