i want to following select tag
<select>
<option value=10> 10% </option>
<option value=20> 20% </option>
<option value=30> 30% </option>
</select>
using
((0..10).to_a.collect {|r| ["#{r*10} %", r*10] })
will give structure like
[["0 %", 0], ["10 %", 10], ["20 %", 20], ["30 %", 30]]
i wonder if this structure can be converted to something like this
[#<ratio id: 10, name: "10%">, #<id: 20, name:"20%">]
how to convert first structure to second strcuture
so i could use
options_from_collection_for_select(@arr,"id","name")
to populate the select_tag
or
is there any better way to do the same?
comments please.
You can try to use OpenStruct for this.
But if you need a real class, not anonymous class like OpenStruct, then just create this class instances inside the collect/map method:
Maybe I misunderstood you a bit and you want to create this structure not from range, but from array
[["0 %", 0], ["10 %", 10], ["20 %", 20], ["30 %", 30]]then the code is similar:UPDATE
I found one corner case in usage of OpenStruct (looks like this is the case for Ruby 1.8.7 only):
OpenStruct#idwill returnobject_idforidinstead of field value:There are two workarounds:
I recommend you to use second approach.