4

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!

Tony
  • 18,776
  • 31
  • 129
  • 193

6 Answers6

9

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/

BushyMark
  • 3,315
  • 2
  • 21
  • 22
  • yes i have seen that as well, but it doesn't do the job in one select. i should be finished coding the solution in an hour or so. i'll post it soon – Tony Apr 20 '09 at 22:28
  • 1
    I modified the above answer to work with Rails 3, and to have 12 am instead of 0 am. See: https://gist.github.com/1340125. – David van Geest Nov 04 '11 at 18:44
7

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
sean_j_roberts
  • 430
  • 7
  • 9
2

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

Andrew Vit
  • 18,961
  • 6
  • 77
  • 84
dave elkins
  • 331
  • 2
  • 4
2

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

<%= f.time_select :attribute_name, :minute_step => 15, :ampm => true %>
Igbanam
  • 5,904
  • 5
  • 44
  • 68
1

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.

Dave Powers
  • 2,051
  • 2
  • 30
  • 34
Eric Hurst
  • 11
  • 1
1

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.

Matt Grande
  • 11,964
  • 6
  • 62
  • 89