help creating array/struct for select tag

here is my current code

<%= select_tag(:order, options_for_select(@orders, params[:order].to_s))
%>

I am using some data that is not in a database so I generated a hash
like the following:

@orders = {'Name => 'name', 'Rank' => 'rank', 'HP' => 'hp', 'SP' =>
'sp'} etc...

it works fine but the problem I have is that the hash is never in the
same order...after reading it looks like i should be using a struct or
an array.

can someone help me convert this hash into an array or suggest the best
way to do this?

Hi Scott!

I am using some data that is not in a database so I generated a hash
like the following:

@orders = {'Name => 'name', 'Rank' => 'rank', 'HP' => 'hp', 'SP' =>
'sp'} etc...

it works fine but the problem I have is that the hash is never in the
same order...after reading it looks like i should be using a struct or
an array.

AFAIK hashes are *not* ordered *by definition*.

I know this may not be very helpful but it may explain *why* you
experience this behaviour.

Kind regards
Martin

<%= select_tag(:order, options_for_select(@orders, params[:order].to_s))
%>

I am using some data that is not in a database so I generated a hash
like the following:

@orders = {'Name => 'name', 'Rank' => 'rank', 'HP' => 'hp', 'SP' =>
'sp'} etc...

it works fine but the problem I have is that the hash is never in the
same order...after reading it looks like i should be using a struct or
an array.

can someone help me convert this hash into an array or suggest the best
way to do this?

@orders = [['Name', 'name'], ['Rank', 'rank'], ....]

Should do it. Or you might need to swap 'name' with 'Name'. I can never remember :slight_smile:

-philip

@orders = [['Name', 'name'], ['Rank', 'rank'], ....]

Should do it. Or you might need to swap 'name' with 'Name'. I can
never remember :slight_smile:

-philip

hmm..i don't think it likes it.

it's outputting this:

<select id="order" name="order">
<option value="Namename">Namename</option>
<option value="Rankrank">Rankrank</option>
etc...

i tried doing this:

<%= select_tag(:order, options_for_select(@orders.collect{|order|
[order[0], order[1]]}, params[:direction].to_s)) %>

but ended up with this output:

<select id="order" name="order">
<option value="">Namename</option>
<option value="">Rankrank</option>
etc...

Do you really need the difference in case? If not, try

  @orders = %w(Name Rank SP HP)

I believe that will work...

@orders = [['Name', 'name'], ['Rank', 'rank'], ....]

Should do it. Or you might need to swap 'name' with 'Name'. I can
never remember :slight_smile:

-philip

hmm..i don't think it likes it.

Strange. It should. Docs say:

options_for_select([["Dollar", "$"], ["Kroner", "DKK"]])
     <option value="$">Dollar</option>\n<option value="DKK">Kroner</

Roy Pardee wrote:

Do you really need the difference in case? If not, try

  @orders = %w(Name Rank SP HP)

I believe that will work...

Yea, actually that does work fine...i'm trying to order by that field in
the database and i thought it was case sensitive in the SQL query.

however, i do have another sort field

@directions = {'Ascending' => 'ASC', 'Descending' => 'DESC'}

and ASC and DESC might not be descriptive enough if you have any other
ideas.

thanks for the help so far!