Collections and hardcoded options

Hello all.

I am having a bit of a mind blank and was wondering if anyone could give
me a hand.

I am planning on grouping users. These groups will be hardcoded
somewhere. When I create a user I will assign them to a group based on a
dropdown (which is currently totally HTML, including the options) which
gets saved to the user profile.

This works fine however when modifying the user the group isn’t being
picked up of course because it is hardcoded HTML.

So what I am trying to figure out is. Create a dropdown that lists all
the hardcoded groups from an array in the code and defaults the dropdown
to the one that is associated with the user profile.

There aren’t a large number of groups and they aren’t going to change
which is why I am not sticking them in a table.

Cheers

RJ

RJ,

Use the ActionView method select. 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} %>

Marc L. wrote:

RJ,

Use the ActionView method select. 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} %>

Many thanks Marc! As you explained it I was in forehead slapping mode. I
have a psychological problem dealing with selects and collections at the
best of times so I think my brain involuntarily shutdown when confronted
with a non-standard (to my experience) situation.

Thank you very much for the detailed explanation.

RJ

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

This is great!

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

Thanks.
Ivor