I''ve got one form where someone there is an array of text fields, allowing the user to enter a several foos. I''d like to process these parameters as an array. I''ve noticed that if the field is named foo[x], then I can reference it as "params[:foo][''x'']". I was just wondering if there was someway to tell Rails I''d like params[:foo] to be an array not a hash. Thanks.
On 8-jul-2005, at 10:53, David Corbin wrote:> I''ve got one form where someone there is an array of text fields, > allowing the > user to enter a several foos. I''d like to process these parameters > as an > array. I''ve noticed that if the field is named foo[x], then I can > reference > it as "params[:foo][''x'']". > > I was just wondering if there was someway to tell Rails I''d like > params[:foo] > to be an array not a hash.Yes. Use the [] notation instead of specifying a key (i.e. foo[]). However, I use numbers as hash keys and then just remap them in the controller. -- Julian "Julik" Tarkhanov
On Saturday 09 July 2005 09:07 pm, Julian ''Julik'' Tarkhanov wrote:> On 8-jul-2005, at 10:53, David Corbin wrote: > > I''ve got one form where someone there is an array of text fields, > > allowing the > > user to enter a several foos. I''d like to process these parameters > > as an > > array. I''ve noticed that if the field is named foo[x], then I can > > reference > > it as "params[:foo][''x'']". > > > > I was just wondering if there was someway to tell Rails I''d like > > params[:foo] > > to be an array not a hash. > > Yes. Use the [] notation instead of specifying a key (i.e. foo[]). > However, I use numbers as hash keys and then just remap them in the > controller.What I think you''re saying, is write this method: def foo input = [] params[:foo].each {|k,v| input[k.to_i] = v} input end Is that correct? David
On 10-jul-2005, at 3:25, David Corbin wrote:>> > > What I think you''re saying, is write this method: > > def foo > input = [] > params[:foo].each {|k,v| input[k.to_i] = v} > input > end > > Is that correct? > DavidYes. I just don''t think it does deserve a method. Actually I even think ActiveSupport must include some handy method for this, but (surely enough) I don;t know about it and it''s too late to go all over the AS source. But if you look you might find it readymade there. -- Julian "Julik" Tarkhanov