Forum: Ruby on Rails Selecting time

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
08d1d61c507f9af92d2b26af440bb0da?d=identicon&s=25 Arjen Roodselaar (froggie)
on 2005-12-24 00:33
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?
61b70c2f195b0e669a8e25000148d9dd?d=identicon&s=25 Eden Brandeis (Guest)
on 2005-12-24 07:14
(Received via mailing list)
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.
08d1d61c507f9af92d2b26af440bb0da?d=identicon&s=25 Arjen Roodselaar (froggie)
on 2005-12-25 12:40
Eden Brandeis 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 :)

Anybody else who is experiencing my 'problem'?
364025223fa593e2e76111b31d76f406?d=identicon&s=25 Piet Hadermann (piet)
on 2005-12-25 16:44
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 :)
>
> 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 <select name="starttime[hour]"> and <select
name="starttime[minute]">.

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.
08d1d61c507f9af92d2b26af440bb0da?d=identicon&s=25 Arjen Roodselaar (Guest)
on 2005-12-25 20:56
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!
364025223fa593e2e76111b31d76f406?d=identicon&s=25 Piet Hadermann (piet)
on 2005-12-25 22:38
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?):

<p><label for="user_starttime">Start Time</label><br/>
<%= select_time (@user.starttime, :prefix => 'starttime')  %></p>

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 ;)

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.
08d1d61c507f9af92d2b26af440bb0da?d=identicon&s=25 Arjen Roodselaar (Guest)
on 2005-12-26 13:22
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.
This topic is locked and can not be replied to.