Forum: Ruby on Rails Multiple Select example?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F0379e983ab499fac4d972494beab5c0?d=identicon&s=25 Brez! !! (brez)
on 2006-04-26 20:23
I've been trying to sort out a multiple select dropdown but haven't
found any good examples - anyone know of any or perhaps could make a
suggestion [see below]

Model:
1. Task AR has_many users
2. User AR belongs_to task
   login/password attributes

Controller:


View:
<%= collection_select("task", "users" , @users, "id", "login", {},
{:multiple => true}) %>

 def new
   @task = Task.new
   @users = User.find_all
 end

 def create
   @task = Task.new(params[:task])

   if @task.save
     flash[:notice] = 'Task was successfully created.'
     redirect_to :action => 'list'
   else
     render :action => 'new'
   end
 end

Error:

"User expected, got String"


Seems pretty straight forward but isn't working - any sugggestions?

Thanks.
F0379e983ab499fac4d972494beab5c0?d=identicon&s=25 Brez! !! (brez)
on 2006-04-27 09:32
Found it!  FYI if it comes up again:

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

Michael Koziarski wrote:
| Nice, are there any functions like this for handling one to many
| relations?  i.e a User has_and_belongs_to_many :groups.

collection_select requires a source object and an instance method.  With
has_many, this method can be the foreign key.  But with
has_and_belongs_to_many there is no corresponding instance method since
the keys are in a separate join table, so you'll want to manage the
parameters yourself.  Here's an example using
options_from_collection_for_select to create a multiple select:

class User < ActiveRecord::Base
~  # id, name fields
~  has_and_belongs_to_many :groups
end

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

class UserController < AbstractApplicationController
~  model :user
~  def edit
~    case @request.method
~    when :get
~      @user = User.find(@params['id'])
~      @available_groups = Groups.find_all - @user.groups
~    when :post
~      @user = User.find(@params['user']['id'])
~      @user.groups << Group.find(@params['add_groups'])
~      redirect_to :action => 'show', :id => @user.id
~    end
~  end
end

views/user/edit.rhtml
<%= form_tag :action => 'edit' %>
~  <%= hidden_field 'user', 'id' %>
~  <select name="add_groups[]" multiple="multiple">
~    <%= options_from_collection_for_select @available_groups, 'id',
'name' %>
~  </select>
</form>


Check out the relevant FormOptionsHelper documentation:
http://ap.rubyonrails.org/classes/ActionView/Helpe...

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

iD8DBQFBpBYeAQHALep9HFYRAk8qAKCbRIzv2GBiWGSfIsmlx1ZZYuodqQCePlV2
9tqGiLfiZTyocwJ1U9xMTmY=
=vrPm
-----END PGP SIGNATURE-----
F0379e983ab499fac4d972494beab5c0?d=identicon&s=25 Brez! !! (brez)
on 2006-04-27 21:02

Actually - here's my working sample [a little cleaner then above]

view:

   <select name="task[user_ids][]" multiple="multiple">
     <%= options_from_collection_for_select @users, 'id', 'login' %>
   </select>

controller:

    @task = Task.new(@params[:task])


Turns out I missed this very valuable wiki entry - thanks Joel!

http://wiki.rubyonrails.com/rails/pages/HowtoUseFo...
This topic is locked and can not be replied to.