Getting a time select input


#1

I’m having trouble workign out which ActionView DateHelpers to display a
time input for an ActiveRecord field of type :time.

I just want an hours input and a minuites input. I can get these with
time_select helper using :field_name and :prefix but it gives both hours
and miniutes the same name.

<%= select_time(Time.now, :prefix => “search”, :field_name =>
“start_time”) %>

I see when you use the datetime_select it gives each element something
like 1i, 2i, 3i and 4i, this does not happen with select_time.

The datetime_select would work if I could get rid of the date part,
there is no time_select helper! And I have tried adding the :discard
option but it does not get rid of the year…

<%= datetime_select(“search”, “start_time”, { :discard_year => true,
:discard_month => true, :discard_day => true }) %>

Can anyone clue me in :slight_smile:

Kris.


#2

Excellent, I actually tried something similar but could’nt get it to
work, I like Time.parse, I was not aware of it :slight_smile: Many thanks.

It suprised me there was not a data aware Time select helper actually.

I dont know if you noticed this but when you store a time and then print
it to screen you also get a date?

@search.start_time.class # => Time

<%= @search.start_time %> # => Wed Mar 22 11:06:00 GMT Standard Time
2006

Josh S. wrote:

<%= select_time(Time.now, :prefix => “start_time”) %>

Then, when you get to your action, you have to take the params for the
time selection and massage them into params for the model. For a Foo
model, do this:

params[:foo][:start_time] = Time.parse(params[:start_time][:hour] + “:”

  • params[:start_time][:minute])

Then you can do the usual foo = Foo.new(params[:foo]) and you’ll get the
start_time as part of the bargain.


#3

Kris wrote:

I’m having trouble workign out which ActionView DateHelpers to display a
time input for an ActiveRecord field of type :time.

I just want an hours input and a minuites input. I can get these with
time_select helper using :field_name and :prefix but it gives both hours
and miniutes the same name.

<%= select_time(Time.now, :prefix => “search”, :field_name =>
“start_time”) %>

I see when you use the datetime_select it gives each element something
like 1i, 2i, 3i and 4i, this does not happen with select_time.

The datetime_select would work if I could get rid of the date part,
there is no time_select helper! And I have tried adding the :discard
option but it does not get rid of the year…

<%= datetime_select(“search”, “start_time”, { :discard_year => true,
:discard_month => true, :discard_day => true }) %>

Can anyone clue me in :slight_smile:

Kris.

The select_time helper is not model aware, so you have to do a bit more
work to use it. I just had to deal with this a few days ago, and this is
what I came up with.

You are almost there… drop the :field_name option as that won’t help
you (it changes [hour] and [minute] to [start_time]). Keep the :prefix
option, and set it to the name of the attribute of your model object.

<%= select_time(Time.now, :prefix => “start_time”) %>

Then, when you get to your action, you have to take the params for the
time selection and massage them into params for the model. For a Foo
model, do this:

params[:foo][:start_time] = Time.parse(params[:start_time][:hour] + “:”

  • params[:start_time][:minute])

Then you can do the usual foo = Foo.new(params[:foo]) and you’ll get the
start_time as part of the bargain.

If you figure out a better way to handle this, please let me know. My
approach works fine, but it’s not very pretty.

It looks like there is a patch in the works to add a time_select helper
that is model aware, but it hasn’t had much love lately so it might be a
while before it’s ready for use.

–josh
http://blog.hasmanythrough.com


#4

Kris wrote:

The datetime_select would work if I could get rid of the date part,
there is no time_select helper! And I have tried adding the :discard
option but it does not get rid of the year…

<%= datetime_select(“search”, “start_time”, { :discard_year => true,
:discard_month => true, :discard_day => true }) %>

We were discussing it on IRC and it appeared that datetime_select has a
bug with not discarding a year field. The patch was already submitted
and I believe it is fixed in Rails trunk.


#5

Thanks Maxim, Im on Rails 1.0.0 at the moment, may give the trunk a go
Which would be better than my method of getting putting the correct
values back in to the Time inputs on a failed save. I store the time
values in the session before trying to save the record. So if it fails
the edit view can get the time values entered already from the session.

Maxim K. wrote:

Kris wrote:

The datetime_select would work if I could get rid of the date part,
there is no time_select helper! And I have tried adding the :discard
option but it does not get rid of the year…

<%= datetime_select(“search”, “start_time”, { :discard_year => true,
:discard_month => true, :discard_day => true }) %>

We were discussing it on IRC and it appeared that datetime_select has a
bug with not discarding a year field. The patch was already submitted
and I believe it is fixed in Rails trunk.


#6

Kris wrote:

Excellent, I actually tried something similar but could’nt get it to
work, I like Time.parse, I was not aware of it :slight_smile: Many thanks.

It suprised me there was not a data aware Time select helper actually.

I dont know if you noticed this but when you store a time and then print
it to screen you also get a date?

@search.start_time.class # => Time

<%= @search.start_time %> # => Wed Mar 22 11:06:00 GMT Standard Time
2006

Josh S. wrote:

<%= select_time(Time.now, :prefix => “start_time”) %>

Then, when you get to your action, you have to take the params for the
time selection and massage them into params for the model. For a Foo
model, do this:

params[:foo][:start_time] = Time.parse(params[:start_time][:hour] + “:”

  • params[:start_time][:minute])

Then you can do the usual foo = Foo.new(params[:foo]) and you’ll get the
start_time as part of the bargain.

I just found a better way:
<%= select_time(Time.now, :prefix => “calendar”, :field_name =>
'start_time(4i)) %>

If you do that then you don’t have to modify your update to save the
time. In my case I am doing a date_select for the rest of it (I can’t
use datetime_select because the :order option does not work for it)

This will only take the minutes, so I really had to do it twice, once
with
select_minutes and once with select_hours

like this:

    <%= date_select 'event', 'event_at', :order => [:month, :day,

:year] %>
Time
<%= select_hour(Time.now, :prefix => “event”, :field_name =>
‘event_at(4i)’, :ampm => true) %>
<%= select_minute(Time.now, :prefix => “event”, :field_name =>
‘event_at(5i)’, :minute_step => 15) %>