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.
05b876a1f205e76fdce882024e0e1630?d=identicon&s=25 Richard (Guest)
on 2005-11-21 23: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
648d60296e65a1278017c0b9f960ec3a?d=identicon&s=25 ol1barn3s (Guest)
on 2005-11-22 00: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 <richard.mcgrath@gmail.com>:
2ee1028723473faa763884e902ce3366?d=identicon&s=25 thiessen (Guest)
on 2005-11-22 02: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
05b876a1f205e76fdce882024e0e1630?d=identicon&s=25 Richard <richard.mcgrath@gmail.com> (Guest)
on 2005-11-22 02: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 <richard.mcgrath@gmail.com>:
C9c7a65848f13e2b1a226bbe43bc3672?d=identicon&s=25 Pete Yandell (pete)
on 2005-11-22 05: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 Yandell
F9646277392b8d8d718ef457696a0423?d=identicon&s=25 ryan.raaum (Guest)
on 2005-11-22 06:01
(Received via mailing list)
or just (1..26).to_a

-r

--
Ryan Raaum
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
648d60296e65a1278017c0b9f960ec3a?d=identicon&s=25 ol1barn3s (Guest)
on 2005-11-22 18:06
(Received via mailing list)
hehehe

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

2005/11/22, Ryan Raaum <ryan.raaum@gmail.com>:
3eda6fcd3204ef285fa52176c28c4d3e?d=identicon&s=25 mixplate (Guest)
on 2007-03-02 02:09
thank goodness for search feature working well!
3eda6fcd3204ef285fa52176c28c4d3e?d=identicon&s=25 mixplate (Guest)
on 2007-03-02 03: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
Ba119f28a4f94fa7bdd919857d0c9432?d=identicon&s=25 9by11valulabs (Guest)
on 2007-06-09 13: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.