views:

1530

answers:

5

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!

A: 

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
+3  A: 

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
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
+2  A: 

Ok everyone, here is the code for the most compact Ruby on Rails time_select. Enjoy...

http://www.tonyamoyal.com/2009/04/21/simple-time-select-the-most-compact-time_select-for-ruby-on-rails/

Tony
A: 

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
SeanLazer
+1  A: 

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

dave elkins