Selecting time

I’m currently building a small app that’s going to be used to keep track
of the amount of time spend on customers of the company I work for.
This mainly consists of object with a begin time, an end time and a
calculation of the difference between those timestamps (actual time
spend on the job).

I made a small form with a datetime_select helper for the begin and end
time (the column type inside the DB is also dateselect). Because the
actual time spend on the job can be less then the difference between
begin and end I want this time to be selectable too. Because this
attribute only holds hours and minutes I tried to use the select_time
helper. Unfortunately there seems to be a small bug in this helper cause
the fieldname that’s being parsed is always date[“name you give to it
using the :field_name option”].
I just tried the datetime_select using the :discard_year and -month
options, but this helper always needs the year selection field (probably
has something to do with the fact that Time.gm() requires at least the
year argument). Although this works (the value inside the ‘time’ column
is nicely being mapped to the ‘time’ field it just doesn’t look right
with the year selection field being displayed.

The question is: is there an easy and elegant way to solve this problem
or will I have to build my own time_select helper using the hour and
minute field?

Have you seen http://www.formassembly.com/time-tracker/ ?

I think it is a nice solution to the problem you are trying to solve.
Of
course if you want it to be local or want to solve the problem for the
purpose of learning, that is a different story.

Eden B. wrote:

Have you seen http://www.formassembly.com/time-tracker/ ?

It looks like a nice standalone application, but I’m writing this app so
it can be integrated in our intranet and ticketsystem. Besides that, I’m
using this project to give myself a crash course RoR :slight_smile:

Anybody else who is experiencing my ‘problem’?

Arjen Roodselaar wrote:

It looks like a nice standalone application, but I’m writing this app so
it can be integrated in our intranet and ticketsystem. Besides that, I’m
using this project to give myself a crash course RoR :slight_smile:

Anybody else who is experiencing my ‘problem’?

Yes, I noticed the exact same thing using select_time and the
:field_name option. I solved this by using the :prefix option to set the
field name to something else (in my case ‘starttime’).
My html source now shows and .

In the create function in my controller I do the following to get the
correct time (starttime is a mysql ‘time’ column in a ‘users’ table):

@user.starttime = Time.gm(2005,1,1,@params[‘starttime’][‘hour’],
@params[‘starttime’][‘minute’])

I’m a rails newbie, so I don’t know if this is the correct ‘rails way’
to handle this, it worked for me but it seemed cumbersome. Is there any
info anywhere on how I can make a select_time helper myself to work just
like the datetime_select helper ?

While doing this, being new to rails (and only hacked together a couple
of lines of code in ruby), I wondered why params[:starttime][:minute]
seemed to be the same as @params[‘starttime’][‘minute’]. Is it the same
? Which notation is preferred ?

Piet.

Piet, thanks for your input. I was already thinking about doing it the
same way, but I’m still having problems with my ‘edit’ action (the
values are not being put back into the form). If you happened to solve
this problem, sharing your code would be appreciated!

Piet,

Sorry, you’re right you posted your code. I think I was sleeping
yesterday. I managed to hack up the original datetime_select (wasn’t
that hard after all) to allow just time selection. It’s still a bit
messy, so I want to clean up first before posting. I’ll probably submit
it as a patch to the developers so it can be included in the next
release.

Arjen Roodselaar wrote:

Piet, thanks for your input. I was already thinking about doing it the
same way, but I’m still having problems with my ‘edit’ action (the
values are not being put back into the form). If you happened to solve
this problem, sharing your code would be appreciated!

Well, I just did… here’s what I put in my form (or should I say
view?):

Start Time
<%= select_time (@user.starttime, :prefix => 'starttime') %>

In my controller:

def update
@user = User.find(params[:id])

@user.starttime = Time.gm(2005,1,1,
           @params['starttime']['hour'],
           @params['starttime']['minute'])

if @user.update_attributes(params[:user])

Hope this helps. Since these are my first rails steps I really don’t
know what I’m doing and can’t give you any decent explanation or even
name things the way they’re called. I hope you realize I just handed
over almost 100% of the rails code I made thus far :wink:

I just wished someone would be able to point at how to make a decent
select_time helper function along the lines of how datetime_select
works. It suprises me there’s no standard ActiveRecord way to handle
‘time’ columns. I guess I’ll have to dig into the rails bowels myself…
(or maybe I’m not seeing ‘it’ yet?)

Piet.