Chad W. wrote:
I have an class called time in my libs folder and I want to call an
array from this class and populate a drop down box/select box in rails
in one of my view pages.
everything online is so confusing regarding this…
There are multiple ways of invoking the helpers to produce output.
Just say my array is like this
time = [[minute,min,20,50],[hour,hr,10,50]]
Sorry but I do not understand. Obviously “min” is short for “minute”
but how do 20 and 50 relate? And same for the hours? Whare are the
values of those ‘minute’ and ‘min’ and ‘hour’ and ‘hr’ variables?
How would I get a select box to list
-minute
-second
But you don’t even list “minute” or “second” in your time array.
Let’s try a simple example. Suppose you have a Time object and you
want to set a variable ‘value’ to one of several values.
<%= select(:time, :value, { “One” => 1, “Two” => 2, “Three” => 3 }) %>
That will create html output:
1
2
3
That will display “One”, “Two”, and “Three” in the selection dropdown
box. Upon POST submission this will create a params[:time] array
containing :value as params[:time][:value] containing either 1, 2 or
3. Since it is part of the params[:time] object you can simply create
a new Time in the controller normally.
@time = Time.new(params[:time])
The variable ‘value’ will be assigned automatically to the object.
I used one, two, three since those are illustrative of the technique.
But you can do many other things there. And there is another helper
called collection_select() that is also very useful when dealing with
an array of objects.
The values may also be nested arrays. But I think the previous is
more visible and easier to read. But the following is also possible.
<%= select(:time, :value, [[“One”,1],[“Two”,2],[“Three”,3]]) %>
As to your particular issue:
time = [[minute,min,20,50],[hour,hr,10,50]]
Sorry but I do not understand this example. So instead let me assume
that you have the following:
time = { “Hour” => 3600, “Minute” => 60, “Second” => 1 }
Then the following:
<%= select(:time, :value, time) %>
Will produce this HTML:
3600
60
1
Hope this is helpful!
Bob