Hi, I have built a select field that allows multiple options selected with collection_select and "multiple"=>"multiple". How do I go about receiving the values? If I use a variable name like collection[], it complains that I can''t use @collection[] as the instance variable. TIA victor
Victor Jalencas wrote:> Hi, > > I have built a select field that allows multiple options selected with > collection_select and "multiple"=>"multiple". How do I go about > receiving the values? If I use a variable name like collection[], it > complains that I can''t use @collection[] as the instance variable. > > > TIA > > victorHmm, you may have to assemble the <select> method yourself and then use options_from_collection_for_select, like this: <select name="foo[]" multiple="multiple"> <%= options_for_collection_from_select(@collection, ''name'', ''id'') %> </select> It sounds logical, though, that collection_select could be given an option to produce a multiple select. I think that would be generally useful. Care to take a stab at it? :) -Scott _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Actually, I can create such code. Here it is: <%= collection_select ("collection", "id", @collections,''id'', ''name'', {}, "multiple"=>''multiple'' , "size"=>5) %> However, what I don''t know is what to put in the controller to process the form data. If I leave the code as that, I only receive the first selected item. If I do use "collection[]" as the first parameter, I get this exception: `@collection[]'' is not allowed as an instance variable name victor Scott Barron wrote:> Victor Jalencas wrote: > >> Hi, >> >> I have built a select field that allows multiple options selected >> with collection_select and "multiple"=>"multiple". How do I go about >> receiving the values? If I use a variable name like collection[], it >> complains that I can''t use @collection[] as the instance variable. >> >> >> TIA >> >> victor > > > Hmm, you may have to assemble the <select> method yourself and then use > options_from_collection_for_select, like this: > > <select name="foo[]" multiple="multiple"> > <%= options_for_collection_from_select(@collection, ''name'', ''id'') %> > </select> > > It sounds logical, though, that collection_select could be given an > option to produce a multiple select. I think that would be generally > useful. Care to take a stab at it? :) > > -Scott > > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Victor Jalencas wrote:> Actually, I can create such code. Here it is: > > <%= collection_select ("collection", "id", @collections,''id'', ''name'', > {}, "multiple"=>''multiple'' , "size"=>5) %> > > However, what I don''t know is what to put in the controller to process > the form data. If I leave the code as that, I only receive the first > selected item. If I do use "collection[]" as the first parameter, I get > this exception: > > `@collection[]'' is not allowed as an instance variable name >Right, perhaps I wasn''t very clear. What I was saying is that if collection_select is given the multiple option it should name the select element appropriately, that is "foo[]". As you''re experiencing, collection_select will not currently do this. What it needs is either a separate multiple options, or to scan the html_options for "''multiple'' => ''multiple''" and name the outputted select tag appropriately. -Scott _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Ah, you''re right. I just realized what you mean. So, the form helper should examine html_options and upon finding multiple, rename the variable so that it ends with [] (as opposed to doing it myself, which doesn''t end up right) Well, at least I know how to do it meanwhile, and how to receive the values -- request dump is so grand! victor Scott Barron wrote:> Victor Jalencas wrote: > >> Actually, I can create such code. Here it is: >> >> <%= collection_select ("collection", "id", @collections,''id'', ''name'', >> {}, "multiple"=>''multiple'' , "size"=>5) %> >> >> However, what I don''t know is what to put in the controller to process >> the form data. If I leave the code as that, I only receive the first >> selected item. If I do use "collection[]" as the first parameter, I >> get this exception: >> >> `@collection[]'' is not allowed as an instance variable name >> > > Right, perhaps I wasn''t very clear. What I was saying is that if > collection_select is given the multiple option it should name the select > element appropriately, that is "foo[]". As you''re experiencing, > collection_select will not currently do this. What it needs is either a > separate multiple options, or to scan the html_options for "''multiple'' > => ''multiple''" and name the outputted select tag appropriately. > > > -Scott > > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails