[Rails] Collection Select Example?

Jeremy Kemper jeremy at bitsweat.net
Tue Nov 23 23:46:50 GMT 2004


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Lon Baker wrote:
| Does anyone have an example of the proper usage of collection_select?

class User < ActiveRecord::Base
~  # id, name fields
~  belongs_to :group
end

class Group < ActiveRecord::Base
~  # id, name fields
~  has_many :users
end

class UserController < AbstractApplicationController
~  model :user
~  def edit
~    @user = User.find(@params['id'])
~  end
end

views/user/edit.rhtml
<%= collection_select 'user', 'group_id', Group.find_all, 'id', 'name' %>

This invocation will generate a select tag name="user[group_id]" with
options of form <option value="group.id">group.name</option>.  The
option with group.id == user.group_id will be selected.

Hope this helps,
jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (Darwin)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBo8vpAQHALep9HFYRAl//AJ9Zy4RlrqENap2rH+c4dYRiBP0jHACgnGsV
EP3kybdAZwBbmnd0WppX+hs=
=GUzz
-----END PGP SIGNATURE-----


More information about the Rails mailing list