[Rails] Collection Select Example?
Michael Koziarski
koziarski at gmail.com
Wed Nov 24 00:23:56 GMT 2004
Nice, are there any functions like this for handling one to many
relations? i.e a User has_and_belongs_to_many :groups.
On Tue, 23 Nov 2004 15:46:50 -0800, Jeremy Kemper <jeremy at bitsweat.net> wrote:
> -----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-----
>
>
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
--
Cheers
Koz
More information about the Rails
mailing list