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.
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.
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:
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?
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</option>"+ options_for_select(@time_values) %>
contact me to my email, booking2heaven[at]yahoo.com, I am always
interested with ruby on rails. I am not good in Java and middle
intermediate in PHP. Thank you.