Newbie question: Populating a selection list with numbers


#1

Hi,

What is a nice, easy, one-line method of populating a selection list
with numbers in rails?

So far I can populate an array with the numbers I want using a loop:

<% opts = []
26.times do |x|
opts << [x, x]
end
-%>

and then use the opts array for the options in the select helper.

But is there a better way?

Thanks for any suggestions,
Richard


#2

i just learned this

<% opts = Array.new(26) {|x| [x,x]} %>

perhaps it could be used this way too? (not positive)

<%= select ‘talent’, ‘age’, Array.new(26) {|x| [x,x]} %>

hope this helps,
Oliver

2005/11/21, Richard removed_email_address@domain.invalid:


#3

Richard <richard.mcgrath@…> writes:

and then use the opts array for the options in the select helper.

But is there a better way?

Here’s how I do it. . .

<%= select_tag :choice, options_for_select(0…30, @params[:choice]) %>

–Forrest


#4

Thanks Oliver, that worked a treat!

(Using the method inside the select-tag helper function)

Regards,
Richard

ol1barn3s wrote:

i just learned this

<% opts = Array.new(26) {|x| [x,x]} %>

perhaps it could be used this way too? (not positive)

<%= select ‘talent’, ‘age’, Array.new(26) {|x| [x,x]} %>

hope this helps,
Oliver

2005/11/21, Richard removed_email_address@domain.invalid:


#5

It’s probably still not quite what you’re after, but you can shorten
your code to:

 (1..26).collect {|x| [x, x] }

and put that directly inside the call to select.

Cheers,

Pete Y.


#6

hehehe

god bless ruby… still a beginner in it, but already love it

2005/11/22, Ryan R. removed_email_address@domain.invalid:


#7

thank goodness for search feature working well!


#8

actually im stuck again…what is the correct line of code if i
initialize my form with a model called :temp with a filed called
‘num_days’?

i have this but not working

<%= select_tag :temp, options_for_select(0…30, :expnum) %>

i am getting a undefined method `stringify_keys!’ for “5”:String…

thanks


#9

no boss

i am not getting any error

its working fine

by


#10

or just (1…26).to_a

-r


Ryan R.
http://www.rockefeller.edu – Bacterial Pathogenesis and Immunology
http://www.worldmartial.com – Black Belt Instructor
http://locomotive.sourceforge.net – Self contained one-click Rails for
Mac OS X