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
--
Posted via http://www.ruby-forum.com/.
Oliver Barnes
2005-Nov-21 23:43 UTC
Re: Newbie question: Populating a selection list with numbers
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-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> 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
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
Forrest Thiessen
2005-Nov-22 01:34 UTC
Re: Newbie question: Populating a selection list with numbers
Richard <richard.mcgrath@...> writes:> 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?Here''s how I do it. . . <%= select_tag :choice, options_for_select(0..30, @params[:choice]) %> --Forrest
Richard
2005-Nov-22 01:54 UTC
Re: Newbie question: Populating a selection list with number
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-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:-- Posted via http://www.ruby-forum.com/.
Pete Yandell
2005-Nov-22 04:21 UTC
Re: Newbie question: Populating a selection list with numbers
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
On 22/11/2005, at 9:50 AM, Richard wrote:
> 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
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
Ryan Raaum
2005-Nov-22 04:59 UTC
Re: Newbie question: Populating a selection list with numbers
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
Oliver Barnes
2005-Nov-22 16:33 UTC
Re: Newbie question: Populating a selection list with numbers
hehehe god bless ruby... still a beginner in it, but already love it 2005/11/22, Ryan Raaum <ryan.raaum-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> 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 > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
mixplate
2007-Mar-02 01:09 UTC
Re: Newbie question: Populating a selection list with number
thank goodness for search feature working well! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
mixplate
2007-Mar-02 02:32 UTC
Re: Newbie question: Populating a selection list with number
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---