Iterator question

I am trying to create a form dropdown select field and I would like to
use an array to build it.

My code is:

<% states = [“Alabama”, “Alaska”, “Alberta”, “Arizona”,
“Arkansas” ]

states.each do | state | %>
 <option value="<% "#{state}" %>"> <% "#{state}" %></option>
<% end %>
</select>

But I get an empty dropdown.

What should I change to make this happen?

Thanks,
Elle

Try:

states = [‘Alabama’, ‘Alaska’, ‘Denial’].freeze
collection_select(:order, :ship_to_state, states.map{|s| [s, s]}, :first, :first)

It’s air-code (untried, untested), but might get you on the right track.

Not working. Other solution?

Elle

states.each do | state | %>
 <option value="<% "#{state}" %>"> <% "#{state}" %></option>
<% end %>
</select>

But I get an empty dropdown.

Hi Elle,

You seem to be using the ruby tags that don’t output their content
i.e. <% %> Have you tried using <%= %>?

Robin

On Oct 30, 2007 12:14 PM, Robin F. [email protected] wrote:

You seem to be using the ruby tags that don’t output their content
i.e. <% %> Have you tried using <%= %>?

Robin

Also, am I the only one that sees no point in wrapping ‘state’ as a
string in itself? I mean it’s a string anyway. You could achieve the
same with just <%= state %>, making for more readable code – is there
an advantage I am not aware of in using the <%= “#{state}” %> way?


Edd Morgan
http://www.eddm.co.uk
+44 (0) 7805 089097

Perhaps I wasn’t clear:

<% states = [‘Alabama’, ‘Alaska’, ‘Denial’].freeze %>
<%= collection_select(:order, :ship_to_state, states.map{|s| [s, s]}, :first, :first) %>

This should generate a tag for the instance variable :order
that affects the ship_to_state attribute. It should contain a
tag for each state with a value and display element exactly
the same: The name of the state.

What, specifically, is not working?

<<<<<< <option value="<% “#{state}” %>"> <% “#{state}” %>

"> <%= "#{state}" %>

Also, am I the only one that sees no point in wrapping ‘state’ as a
string in itself? I mean it’s a string anyway. You could achieve the
same with just <%= state %>, making for more readable code – is there
an advantage I am not aware of in using the <%= “#{state}” %> way?

No, you’re not. I thought the same thing but was at work and couldn’t
linger over the question too long. I don’t see that there is any
additional benefit.

Robin

Well, my original code works by just adding the = sign.

Silly me.
Thanks guys,

Elle

I’m so silly to forget to add =
as in <%= state %>
I’m not at my computer at the moment. Will try this a bit later.

Cheers,
Elle