Creating pulldowns using loops

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

On 6/3/06, Taylor S. [email protected] 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]}

On Sunday, June 04, 2006, at 3:36 AM, Taylor S. wrote:

Taylor


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

try

<%= select ‘radical’,‘strokes’, (1…14).map %>

_Kevin

<%= 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

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

<%= 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