collections and hardcoded options

RJ,

Use the ActionView method <a href="http://api.rubyonrails.org/classes/
ActionView/Helpers/FormOptionsHelper.html#M000506">select</a>. For
instance, lets say you have three groups: red, green, and blue and the
column in your users table is "group".

<%= select 'users', 'group', {"Red" => "red", "Green" => "green",
"Blue" => "blue"}, {:include_blank => true} %>

The keys (Red, Green, and Blue) are what are displayed in the actual
drop down box. The values (red, green, and blue) are what are stored
in the "group" column of the user's row. By default the select method
will automatically select whatever is in the user's group column. For
instance:

@user = User.find(1)
@user.group #=> "green"

If you were to pull up a page with the above select tag for this user,
the select tag should have the "Green" value automatically selected.

Another note... if you have the possible group values hardcoded
somewhere else in your code, you can replace the hardcoded options
hash with a reference to the variables in your code (however, these
must either be in a hash or an array format ... see docs for more
details). So if you had:

class User < ActiveRecord::Base
  GROUPS = {
    "Red" => "red",
    "Green" => "green",
    "Blue" => "blue"
  }
end

Then you could do:

<%= select 'users', 'group', User.GROUPS, {:include_blank => true} %>

This is great!

class User < ActiveRecord::Base
GROUPS = {
“Red” => “red”,
“Green” => “green”,
“Blue” => “blue”
}
end

Thanks.
Ivor

No problem RJ, I've been there COUNTLESS times myself! :slight_smile: