Skip to content

Instantly share code, notes, and snippets.

@excid3
Created December 23, 2011 06:26
Show Gist options
  • Save excid3/1513370 to your computer and use it in GitHub Desktop.
Save excid3/1513370 to your computer and use it in GitHub Desktop.
combined_time_select

In the view you can do the following:

<%= f.time_select :start_time,
  :combined => true,
  :default => Time.now.change(:hour => 11, :min => 30),
  :minute_interval => 15,
  :time_separator => "",
  :start_hour => 10,
  :end_hour => 14 } %>

This will create a combined time select starting at 10 AM and going till 2 PM with 15 minute intervals with a default of 11:30 AM. This will set the value for the start_time attribute on the object this form was created for.

On the controller side, we need to parse this attribute before we create a new object. combined_time_select provides a nice method for this called parse_time_select!. You can use this in your create action just before you initialize the new model:

def create
  params[:event].parse_time_select! :start_time
  @event = Event.new params[:event]
end

And voila! You're all set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment