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.
Taylor S. (Guest)
on 2006-06-04 05: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
Wilson B. (Guest)
on 2006-06-04 06:02
(Received via mailing list)
On 6/3/06, Taylor S. <removed_email_address@domain.invalid> 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]}
Kevin O. (Guest)
on 2006-06-04 06:11
(Received via mailing list)
On Sunday, June 04, 2006, at 3:36 AM, Taylor S. wrote:
>
>Taylor
>
>--
>Posted via http://www.ruby-forum.com/.
>_______________________________________________
>Rails mailing list
>removed_email_address@domain.invalid
>http://lists.rubyonrails.org/mailman/listinfo/rails

try

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

_Kevin
Kevin O. (Guest)
on 2006-06-04 06:15
(Received via mailing list)
On Sunday, June 04, 2006, at 2:10 AM, Kevin O. 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
Taylor S. (Guest)
on 2006-06-04 06: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
Taylor S. (Guest)
on 2006-06-04 06: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.