Forum: Ruby on Rails Creating pulldowns using loops

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.
8e7e5b934fcbcdfb475072685755457f?d=identicon&s=25 Taylor Strait (Guest)
on 2006-06-04 03:36
Creating looped pulldowns in PHP is simple so I can't figure out why it
seems to elude me in RoR.  I want to create a simple pulldown list with
the options and values "1" through "14."

I have been trying to accomplish this with the .upto method like so:

--START RHTML CODE--
<% options_array = [
1.upto(14) {|i| print "[" i "," i "]"}
] %>

<%= select('radical', 'strokes', options_array) %>

--END CODE--

I have tried variations of parenthesis and quotations to no avail.  At
best, I can generate [1,1] but nothing further.  Of course I could have
just done this by hand in less than a quarter of the time I've been
working on using .upto() but I'm stubborn!  Advice would be greatly
appreciated!

Taylor
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-06-04 04:02
(Received via mailing list)
On 6/3/06, Taylor Strait <taylorstrait@gmail.com> wrote:
>
> <%= select('radical', 'strokes', options_array) %>
>
> --END CODE--
>
> I have tried variations of parenthesis and quotations to no avail.  At
> best, I can generate [1,1] but nothing further.  Of course I could have
> just done this by hand in less than a quarter of the time I've been
> working on using .upto() but I'm stubborn!  Advice would be greatly
> appreciated!

You can't use "print" inside of RHTML templates (well, you can, but
it's ugly, and not as pretty as what you pasted.)

<%= select 'radical', 'strokes', (1..14).to_a %>
..would be one way to do it.  (1..14) is a range of numbers, 1 to 14..
'to_a' turns it into an Array.

Sounds like you're making a Japanese dictionary app?

If you really wanted to use upto, you could do it this way (which I
don't recommend, because it's longer.)
options_array = []
1.upto(14) {|i| options_array << [i,i]}
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-06-04 04:11
(Received via mailing list)
On Sunday, June 04, 2006, at 3:36 AM, Taylor Strait wrote:
>
>Taylor
>
>--
>Posted via http://www.ruby-forum.com/.
>_______________________________________________
>Rails mailing list
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails

try

<%= select 'radical','strokes', (1..14).map %>

_Kevin
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-06-04 04:15
(Received via mailing list)
On Sunday, June 04, 2006, at 2:10 AM, Kevin Olbrich wrote:
>>1.upto(14) {|i| print "[" i "," i "]"}
>>appreciated!
>try
>http://lists.rubyonrails.org/mailman/listinfo/rails
oops, the 'map' isn't necessary

<%= select 'radical','strokes', (1..14) %>

_Kevin
8e7e5b934fcbcdfb475072685755457f?d=identicon&s=25 Taylor Strait (Guest)
on 2006-06-04 04:27
<%= select 'radical','strokes', (1..14) %>

Amazing.  This worked like a charm.  Thanks for all the suggestions.  I
knew RoR was supposed to be faster than PHP.  If only there were some
good resources for beginners!  (yes I have the PickAxe/Agile)  I am
developing a Japanese character app as I learn rails so expect more
rookie questions fomr me.  Thanks a lot!

Taylor
8e7e5b934fcbcdfb475072685755457f?d=identicon&s=25 Taylor Strait (Guest)
on 2006-06-04 04:28
<%= select 'radical','strokes', (1..14) %>

Amazing.  This worked like a charm.  Thanks for all the suggestions.  I
knew RoR was supposed to be faster than PHP.  If only there were some
good resources for beginners!  (yes I have the PickAxe/Agile)  I am
developing a Japanese character app as I learn rails so expect more
rookie questions from me.  Thanks a lot!

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