Ruby on Rails time_select plugins?

5.8k Views Asked by At

Does anybody know any Ruby on Rails time_select plugins that have only one select box? So the select box has entries like "9:00 AM", "9:15 AM", etc.?

I have seen a few plugins, but nothing like this.

Thanks!

6

There are 6 best solutions below

0
On BEST ANSWER

So you want every possible time in 15 minute increments in one select list? I'd really recommend doing it using the standard time_select plugin.

Edited to add I don't know of anything like that, and I don't imagine it'd be out there, you'll probably have to make your own.

0
On

If you're looking to do this with Rails 3, you'll need to assign html_options to an empty string (html_options = ""). build_select requires the select options to be html. Otherwise you will receive can't convert Array into String.

See the build_select documentation.

Just a friendly update. Enjoy.

0
On

Rails ActionView::Helpers::DateHelper class has a solution for this

<%= f.time_select :attribute_name, :minute_step => 15, :ampm => true %>
0
On

Here's a really basic helper, put this in your application helper. You would have to convert the time in your controller/model with Time.parse(params[:your_field]). This probably doesn't solve your problem, but it should at least point you in the right direction.

def time_select_tag(name)
  times = []
  (00..23).each do |i|
    (0..3).each { |inc| times << Time.parse("#{i}:#{inc * 15}") }
  end
  return select_tag name, options_for_select(times.map{|t| t.strftime('%I:%M%p')})
end
2
On

I use time_selects ALOT in my apps, and solve time select problems with two minor adjustments.

The first is minute_step:

f.time_select :start_of_shift, :minute_step => 15

This will cut that heinous minute select box into a very managable size (choose your own!)

Also, I found a ruby module that I place i my initializer folder of all my time based apps:

module ActionView
module Helpers
class DateTimeSelector
def select_hour_with_twelve_hour_time 
  datetime = @datetime
  options = @options
  return select_hour_without_twelve_hour_time(datetime, options) unless options[:twelve_hour].eql? true
  if options[:use_hidden]
    build_hidden(:hour, hour)
  else
    val = datetime ? (datetime.kind_of?(Fixnum) ? datetime : datetime.hour) : ''
    hour_options = []
    0.upto(23) do |hr|
      ampm = hr <= 11 ? ' AM' : ' PM'
      ampm_hour = hr == 12 ? 12 : (hr / 12 == 1 ? hr % 12 : hr)              
      hour_options << ((val == hr) ?               
      %(<option value="#{hr}" selected="selected">#{ampm_hour}#{ampm}</option>\n) :               
      %(<option value="#{hr}">#{ampm_hour}#{ampm}</option>\n)             
      )
    end           

    build_select(:hour, hour_options)         
  end       
  end       
  alias_method_chain :select_hour, :twelve_hour_time     
  end   
end
end

I wish I could remember where I found it so I could credit the source, but in a nutshell, it allows me to specify 12-hour time breaking down my time_select fields to two easy selects that are very easy to manage.

f.time_select :start_of_shift, :twelve_hour => true, :minute_step => 15

EDIT: Found the source of the ruby file! Find it here: http://brunomiranda.com/past/2007/6/2/displaying_12_hour_style_time_select/

0
On

Have you looked at 12_hour_time? This has worked for me in the past.