The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
User Friendly Time Entry

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Bob Silva

Posts: 94
Nickname: bobsilva
Registered: Feb, 2006

Bob Silva is a Rails Developer for the UMESD
User Friendly Time Entry Posted: Apr 22, 2006 3:24 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Bob Silva.
Original Post: User Friendly Time Entry
Feed Title: Rails Video Tutorials
Feed URL: http://www.railtie.net/xml/rss/feed.xml
Feed Description: A growing collection of screencasts that show you how to use the many facets of the wonderful world of rails.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Bob Silva
Latest Posts From Rails Video Tutorials

Advertisement
Have a need to track time spent on something? Here's an easy way to allow your users to enter their time in a smart way (anyway they want). This example accepts fractional hours, overflowing minutes and converts and displays them as the user would expect. They are stored in your database as minutes (integer) and displayed as hours/minutes (regardless of how they were input).

Model Code (model.rb):

def set_travel_time(hours, minutes)
  self.travel_time = ((hours.to_f * 60) + minutes.to_i).to_i
end

def get_travel_time
  travel_time.to_i.divmod(60)
end


Controller Code (models_controller.rb):

def create
  @model.new(...)
  ...
  @model.set_travel_time(params[:hours], params[:minutes])
  ...
  if @model.save
  ...
end

def edit
  @model = Model.find(...)
  ...
  @hours, @minutes = @model.get_travel_time
  ...
end


View Code (_form.rhtml):

<%= text_field_tag 'hours', @hours -%> hours 
<%= text_field_tag 'minutes', @minutes -%> minutes 

Read: User Friendly Time Entry

Topic: RadRails was SadRails (for a little while) Previous Topic   Next Topic Topic: ZenTest Reaches 1000 Downloads

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use