<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>I.NFECTIO.US: User Friendly Time Entry</title>
    <link>http://i.nfectio.us/articles/2006/04/22/user-friendly-time-entry</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>"Nothing in life is worth doing if you have no life while doing it"</description>
    <item>
      <title>User Friendly Time Entry</title>
      <description>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).
&lt;br /&gt;&lt;br /&gt;
&lt;p&gt;Model Code (model.rb):
&lt;div style="margin:10px;border: 1px solid #8DBA47; width:500px; height:160px; overflow:scroll; background-color: #F1F8E4;"&gt;
&lt;pre&gt;
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
&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;br /&gt;&lt;p&gt;
Controller Code (models_controller.rb):
&lt;div style="margin:10px;border: 1px solid #8DBA47; width:500px; height:295px; overflow:scroll; background-color: #F1F8E4;"&gt;
&lt;pre&gt;
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
&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;br /&gt;&lt;p&gt;View Code (_form.rhtml):
&lt;div style="margin:10px;border: 1px solid #8DBA47; width:500px; height:70px; overflow:scroll; background-color: #F1F8E4;"&gt;
&lt;pre&gt;
&amp;lt;%= text_field_tag 'hours', @hours -%&gt; hours 
&amp;lt;%= text_field_tag 'minutes', @minutes -%&gt; minutes 
&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;</description>
      <pubDate>Sat, 22 Apr 2006 01:49:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:4ed7da7f7823eacfc7091424e76326d3</guid>
      <author>Bob Silva</author>
      <link>http://i.nfectio.us/articles/2006/04/22/user-friendly-time-entry</link>
      <category>Ruby on Rails</category>
    </item>
  </channel>
</rss>
