Drop down box in ruby

Hello I've only been playing with ruby and rails for a few short weeks,

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..

Just say my array is like this time = [[minute,min,20,50],[hour,hr,10,50]]

How would I get a select box to list -minute -second

Also where is the most correct places to put the method to call the array in the rails application?

I would really appreciate someone helping me learn how to do this.

How would I get a select box to list -minute -hour

i didn’t understand that…

try to explain me some things… i would be pleased to help.

so, you want a ComboBox that shows what? the values in the array?

if it is so, you may want to populate this array in the controller, and iterate it in the view, adding values for each item in your array.

imagine this:

I have an array of places, that i want to show in a ComboBox in the view.

so, in the controller you create an instance variable, thats your array, and in the view, you`ll put pure HTML and iterate your array adding values

like:

you understand what I mean?

by doing this, rails will use the injected ruby code to iterate trough your form, and will repeat the same code for each item in the array, remember you can work this out…

well… lets try to solve your problem…

lets say you have this array

@time = [[minute,min,20,50],[hour,hr,10,50]]

then in the view you’ll iterate this array:

this way you’ll have a ComboBox that shows whatever is in your array’s item’s 0 index, with the value (what goes with the form) being whatever is in you array’s item’s 1 index.

I hope I’m being helpful…

Lucas Franceschi Equipe de Programação (Automação) SGI Sistemas (049) 9922-3360

Chad Weier 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:

  <select id="time_value" name="time[value]">   <option value="One">1</option>   <option value="Two">2</option>   <option value="Three">3</option>   </select>

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:

  <select id="time_value" name="time[value]">   <option value="Hour">3600</option>   <option value="Minute">60</option>   <option value="Second">1</option>   </select>

Hope this is helpful!

Bob