malcoauri
malcoauri

Reputation: 12179

time_select helper and Time object creating

There is the following code:

  .row
    = f.label       :start_time
    = time_select :model, :start_time, { minute_step: 30 }
  .row
    = f.label           :end_time
    = time_select :model, :end_time, { minute_step: 30 }

time_select generates 2 selects for each copy( 1 for hours and 1 for minutes). As result we have 'model' hash in params with 'start_time(1i)', ... 'start_time(5i)' fields (the same for end_time). I have the following questions:

  1. How can I create a new Time object from this hash?
  2. Is it possible to create only one select for each time_select?

Thanks

Upvotes: 1

Views: 476

Answers (2)

yerforkferchips
yerforkferchips

Reputation: 1985

These helpers are optimised for use with a ActiveRecord model and its attributes. If you have the columns start_time and end_time, you should not need to worry about creating the Time object from the passed params yourself.

What are you specifically trying to achieve?

Upvotes: 0

fivedigit
fivedigit

Reputation: 18672

How can I create a new Time object from this hash?

You can let Rails do the work for you:

= f.time_select(:start_time, minute_step: 30)

When you call update_attributes on your model in the controller, Rails will create the Time object and assign it to the model.

If you use Rails 4, be sure to whitelist :start_time and :end_time using the strong parameters.

Is it possible to create only one select for each time_select?

By default, Rails can't do this. However, you can use the combined_time_select gem to do it for you:

f.time_select(:end_time, combined: true, minute_step: 30)

Be sure to restart your Rails server after installing this gem.

Upvotes: 2

Related Questions