summu
summu

Reputation: 388

setting default timestamp in simple_form_for rails

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

UI looks like this enter image description here

database column enter image description here

Upvotes: 0

Views: 396

Answers (2)

Bartosz Pietraszko
Bartosz Pietraszko

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

bo-oz
bo-oz

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

Related Questions