time_select
time_select(object_name, method, options = {}, html_options = {})
public
Returns a set of select tags (one for hour, minute and optionally second) pre-selected for accessing a specified time-based attribute (identified by method) on an object assigned to the template (identified by object). You can include the seconds with :include_seconds. You can get hours in the AM/PM format with :ampm option.
This method will also generate 3 input hidden tags, for the actual year, month and day unless the option :ignore_date is set to true. If you set the :ignore_date to true, you must have a date_select on the same method within the form otherwise an exception will be raised.
If anything is passed in the html_options hash it will be applied to every select tag in the set.
# Creates a time select tag that, when POSTed, will be stored in the article variable in the sunrise attribute. time_select("article", "sunrise") # Creates a time select tag with a seconds field that, when POSTed, will be stored in the article variables in # the sunrise attribute. time_select("article", "start_time", include_seconds: true) # You can set the <tt>:minute_step</tt> to 15 which will give you: 00, 15, 30, and 45. time_select 'game', 'game_time', { minute_step: 15 } # Creates a time select tag with a custom prompt. Use <tt>prompt: true</tt> for generic prompts. time_select("article", "written_on", prompt: { hour: 'Choose hour', minute: 'Choose minute', second: 'Choose seconds' }) time_select("article", "written_on", prompt: { hour: true }) # generic prompt for hours time_select("article", "written_on", prompt: true) # generic prompts for all # You can set :ampm option to true which will show the hours as: 12 PM, 01 AM .. 11 PM. time_select 'game', 'game_time', { ampm: true }
The selects are prepared for multi-parameter assignment to an Active Record object.
Note: If the day is not included as an option but the month is, the day will be set to the 1st to ensure that all month choices are valid.
Styling question
How do we style the select boxes and “:” somehow within this method?
Follwup: it seems in Rails 2.1, FormBuilder#time_select didn’t pass html_options to this method. and it’s fixed i
2.1 sets UTC time by default
Rails 2.1 sets hour select to UTC time value, not local server time by default. So if you’re not in UTC time zone don’t forget to specify timezone in your config/environment.rb: config.time_zone = 'Vilnius'
Changing time/date separators
@hayafirst — it is possible to remove that “:” or change it to something else. Just pass `:time_separator` option. Inspect ActionView::Helpers::DateTimeSelector#separator method for more.
Set time zone in before filter
To set your time zone you could create a before_filter in your application.rb controller
class ApplicationController < ActionController::Base before_filter :set_timezone def set_timezone Time.zone = 'GMT' end end