Forum: Ruby on Rails Newbie question: Populating a selection list with numbers

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Richard (Guest)
on 2005-11-22 00:50
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
ol1barn3s (Guest)
on 2005-11-22 01:44
(Received via mailing list)
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>:
thiessen (Guest)
on 2005-11-22 03:40
(Received via mailing list)
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
Richard <> (Guest)
on 2005-11-22 03:54
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>:
Pete Y. (Guest)
on 2005-11-22 06:25
(Received via mailing list)
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.
ryan.raaum (Guest)
on 2005-11-22 07:01
(Received via mailing list)
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
ol1barn3s (Guest)
on 2005-11-22 19:06
(Received via mailing list)
hehehe

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

2005/11/22, Ryan R. <removed_email_address@domain.invalid>:
mixplate (Guest)
on 2007-03-02 03:09
thank goodness for search feature working well!
mixplate (Guest)
on 2007-03-02 04:32
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
9by11valulabs (Guest)
on 2007-06-09 15:54
(Received via mailing list)
no boss

 i am not getting any error

 its working fine

 by
This topic is locked and can not be replied to.