Reputation: 388
My simple_form_for has fields
<div><%= f.input :start_time %></div>
<div><%= f.input :end_time %></div>
start_time and end_time are my database columns in postgresql
by default when i open the forms it takes the current time as default as select it on options.
How to set the default time which is 02:14
(current time for start & end time) in the screensort to 00:00
Upvotes: 0
Views: 396
Reputation: 1407
Two ways:
1) Set the actual attributes values at initialization, in controller action:
def new
@event = Event.new(start_time: Time.now.beginning_of_day, end_time: Time.now.beginning_of_day)
end
2) If for some reason you don't want to alter the actual attributes, you can just set the values for inputs in view. Use with caution and sparingly.
<div><%= f.input :start_time, input_html: {value: (f.object.start_time || Time.now.beginning_of_day) } %></div>
<div><%= f.input :end_time, input_html: {value: (f.object.end_time || Time.now.beginning_of_day)} %></div>
Upvotes: 0
Reputation: 2872
You could probably best do this in the controller method for new:
def new
@model = Model.new
@model.start_time = Time.now.beginning_of_day
@model.end_time = Time.now.beginning_of_day
end
If I'm not mistaking, this should set the standard date to today, but at 00:00 hours.
Upvotes: 1