# My own 12hr time select helper

**URL:** <https://www.ruby-forum.com/t/my-own-12hr-time-select-helper/129867>\
**Category:** Rails\
**Created:** [February 22, 2008, 6:34am UTC](https://www.ruby-forum.com/t/my-own-12hr-time-select-helper/129867 "2008-02-22T06:34:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [February 22, 2008, 6:34am UTC](https://www.ruby-forum.com/t/my-own-12hr-time-select-helper/129867/1 "2008-02-22T06:34:13Z")

</div>

Hi there,

I created my own time helper which stores hash values for name, and  
value of 12 hr times in an array. The problem is, I’m lost on how to  
create the drop down menu in the view that utilizes the hash data?  
How do i access the values hash nested in the array?

Here’s the helper that generates the selection options in the drop-  
down menu:

def time\_to\_12hr  
@time\_values = [{:twelve =\> “12 AM”, :twentyfour =\> 0}]  
clock24hr = 0  
designator = ‘AM’  
2.times do  
for number in 1…11  
clock24hr += 1  
@time\_values \<\< {:twelve =\> “#{number}  
#{designator}”, :twentyfour =\> clock24hr}  
end  
if designator == ‘AM’  
@time\_values \<\< {:twelve =\> “12 PM”, :twentyfour =\> 12}  
end  
clock24hr = 12  
designator = ‘PM’  
end  
return @time\_values  
end  
end

I then collect the values in the view like this which would be used by  
the select tag that generates the drop down:

\<% @time = time\_to\_12hr %\>

What would the select tag look like to access :twelve as the name  
and :twentyfour as the value for each option? I can’t get my head  
around it.

Thanks,  
-A

---

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [February 22, 2008, 8:18am UTC](https://www.ruby-forum.com/t/my-own-12hr-time-select-helper/129867/2 "2008-02-22T08:18:08Z")

</div>

I am not sure, the way you mean a helper is correct. because as i know  
helper is used to convert or as a function in view like :

\<% time\_to\_12hr(Time.now) %\>.

If I see your def method above, i prefer to put it in application.rb  
and create before\_filter :time\_to\_12hr in required controller. Because i  
see that all values of array are already stored in @time\_values so you  
not need the script below.

> \<% @time = time\_to\_12hr %\>

This is my way :

* * *

## Application Controller

def time\_to\_12hr

[see the posted original script]

end

* * *

## Indonesia\_controller.rb

class IndonesiaController \< ApplicationController  
before\_filter :time\_to\_12hr

def index  
end

end

* * *

## VIEW example: index.rhtml

\<%= select\_tag “my\_time”, “\<option value="none"\>Select Your  
Time”+ options\_for\_select(@time\_values) %\>

What do you think ?

~_~_~_~_~_~_~_~_~_~_~_~_~_~  
Reinhart Ariando  
YM:Booking2Heaven  
WEB: [teapoci.blogspot.com](http://teapoci.blogspot.com)  
~_~_~_~_~_~_~_~_~_~_~_~_~_~

---

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [February 22, 2008, 2:29pm UTC](https://www.ruby-forum.com/t/my-own-12hr-time-select-helper/129867/3 "2008-02-22T14:29:58Z")

</div>

Hi there,

I think it makes sense to have this method in the application  
controller as you suggest. The problem I’m still facing is that the  
drop down options look like this:

twelve12 AMtwentyfour0  
twelve1 AMtwentyfour1  
etc…

So its compressing the hash when what I want is the hash value  
of :twelve to be the name of the select option, and the hash value  
of :twentyfour to be the value for each of the options so it displays  
like this:

12 AM  
1 AM  
etc…

And tied to each option name is the corresponding value, 0, 1, etc.  
What needs to change in the select tag to make this happen?

Thanks,  
-A

On Feb 22, 2:18 am, Visit Indonesia 2008 \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [February 22, 2008, 5:27pm UTC](https://www.ruby-forum.com/t/my-own-12hr-time-select-helper/129867/4 "2008-02-22T17:27:35Z")

</div>

I create like this :

def time\_to\_12hr

@time\_values = []

for number in 1…24  
if number \< 13  
@time\_values \<\< “#{number}+AM”  
else  
@time\_values \<\< “#{number-13}+PM” unless number == 24  
@time\_values \<\< “0”  
end  
end  
end

end

…  
VIEW  
…

\<%= select\_tag “my\_time”, “\<option value=“none”\>Select Your  
Time”+ options\_for\_select(@time\_values) %\>

---

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [February 22, 2008, 5:28pm UTC](https://www.ruby-forum.com/t/my-own-12hr-time-select-helper/129867/5 "2008-02-22T17:28:23Z")

</div>

Forget my footer…he…he…he…

~_~_~_~_~_~_~_~_~_~_~_~_~_~  
Reinhart Ariando  
YM:Booking2Heaven  
WEB: [teapoci.blogspot.com](http://teapoci.blogspot.com)  
~_~_~_~_~_~_~_~_~_~_~_~_~_~

---

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [February 22, 2008, 7:27pm UTC](https://www.ruby-forum.com/t/my-own-12hr-time-select-helper/129867/6 "2008-02-22T19:27:07Z")

</div>

Thanks for your help. Great suggestions. Where are you  
based? …and do you freelance? I have an interesting Ruby on Rails  
project that I’m working on that I could use some help on. Let me  
know if you’d like to learn more, and thanks for the help! It’s a  
Twitter-based application for dieting, and it’s already getting a lot  
of traction. I look forward to hearing back from you.

-A

On Feb 22, 11:28 am, Visit Indonesia 2008 \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [February 22, 2008, 7:48pm UTC](https://www.ruby-forum.com/t/my-own-12hr-time-select-helper/129867/7 "2008-02-22T19:48:03Z")

</div>

contact me to my email, booking2heaven[at][yahoo.com](http://yahoo.com), I am always  
interested with ruby on rails. I am not good in Java and middle  
intermediate in PHP. Thank you.

~_~_~_~_~_~_~_~_~_~_~_~_~_~  
Reinhart Ariando  
YM:Booking2Heaven  
WEB: [teapoci.blogspot.com](http://teapoci.blogspot.com)  
~_~_~_~_~_~_~_~_~_~_~_~_~_~
