Given a request like this: /controller/action?attr=one&attr=two&attr=three In the action, params[:attr] is equal to "three". How do I get an array of "attr" with all three values? Thanks, Erik
Try /controller/action?attr[]=one&attr[]=two&attr[]=three
You would have to rename the attr to something like ''attr[]''. So now your request will look lik: /controller/action?attr[]=one&attr[]=two&attr[]=three This automatically turns it into an array ''attr'' which you can see in the debug out for the parameters. On 12/7/05, Erik Hatcher <erik-LIifS8st6VgJvtFkdXX2HpqQE7yCjDx5@public.gmane.org> wrote:> Given a request like this: > > /controller/action?attr=one&attr=two&attr=three > > In the action, params[:attr] is equal to "three". > > How do I get an array of "attr" with all three values? > > Thanks, > Erik > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Dec 7, 2005, at 9:12 AM, Nick Stuart wrote:> You would have to rename the attr to something like ''attr[]''. So now > your request will look lik: > > /controller/action?attr[]=one&attr[]=two&attr[]=three > > This automatically turns it into an array ''attr'' which you can see in > the debug out for the parameters.Thanks for that tip. Though it doesn''t satisfy me :) The Java servlet API deals with this by allowing you to get a single attribute or an array of attributes by a given name. It would be good for Rails to support this multiple attribute case. For now, I''ve numbered the params... attr0=one&attr1=two&attr2=three. Erik> > On 12/7/05, Erik Hatcher <erik-LIifS8st6VgJvtFkdXX2HpqQE7yCjDx5@public.gmane.org> wrote: >> Given a request like this: >> >> /controller/action?attr=one&attr=two&attr=three >> >> In the action, params[:attr] is equal to "three". >> >> How do I get an array of "attr" with all three values? >> >> Thanks, >> Erik >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Dec 7, 2005, at 7:59 AM, Erik Hatcher wrote:> > On Dec 7, 2005, at 9:12 AM, Nick Stuart wrote: >> You would have to rename the attr to something like ''attr[]''. So now >> your request will look lik: >> >> /controller/action?attr[]=one&attr[]=two&attr[]=three >> >> This automatically turns it into an array ''attr'' which you can see in >> the debug out for the parameters. > > Thanks for that tip. > > Though it doesn''t satisfy me :) > > The Java servlet API deals with this by allowing you to get a > single attribute or an array of attributes by a given name. It > would be good for Rails to support this multiple attribute case. > For now, I''ve numbered the params... attr0=one&attr1=two&attr2=three.Isn''t that what Nick just demonstrated? What about it is unsatisfactory? With /controller/action?attr[]=one&attr[]=two&attr[]=three You can do params[:attr] # => ["one", "two", "three"] - Jamis> > Erik > > >> >> On 12/7/05, Erik Hatcher <erik-LIifS8st6VgJvtFkdXX2HpqQE7yCjDx5@public.gmane.org> wrote: >>> Given a request like this: >>> >>> /controller/action?attr=one&attr=two&attr=three >>> >>> In the action, params[:attr] is equal to "three". >>> >>> How do I get an array of "attr" with all three values? >>> >>> Thanks, >>> Erik >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Dec 7, 2005, at 10:01 AM, Jamis Buck wrote:> On Dec 7, 2005, at 7:59 AM, Erik Hatcher wrote: > >> >> On Dec 7, 2005, at 9:12 AM, Nick Stuart wrote: >>> You would have to rename the attr to something like ''attr[]''. So now >>> your request will look lik: >>> >>> /controller/action?attr[]=one&attr[]=two&attr[]=three >>> >>> This automatically turns it into an array ''attr'' which you can >>> see in >>> the debug out for the parameters. >> >> Thanks for that tip. >> >> Though it doesn''t satisfy me :) >> >> The Java servlet API deals with this by allowing you to get a >> single attribute or an array of attributes by a given name. It >> would be good for Rails to support this multiple attribute case. >> For now, I''ve numbered the params... attr0=one&attr1=two&attr2=three. > > Isn''t that what Nick just demonstrated? What about it is > unsatisfactory? With > > /controller/action?attr[]=one&attr[]=two&attr[]=three > > You can do > > params[:attr] # => ["one", "two", "three"]It''s unsatisfactory because it makes ugly URLs! :) There is really no technical reason the [] need to be on the attributes - it only has to do with Rails squashing things into a Hash otherwise, squeezing out all but the last attribute of a given name. Erik
ara.t.howard-32lpuo7BZBA@public.gmane.org
2005-Dec-07 16:34 UTC
Re: multiple same-named request parameters
On Wed, 7 Dec 2005, Nick Stuart wrote:> You would have to rename the attr to something like ''attr[]''. So now > your request will look lik: > > /controller/action?attr[]=one&attr[]=two&attr[]=three > > This automatically turns it into an array ''attr'' which you can see in > the debug out for the parameters.in the past i''ve modified cgi.rb to modify parameter object to support this kind of usage: params["foo"] #=> "bar_0" params["foo"].list #=> [ "bar_0", "bar_1", "bar_2" ] i wonder what people would think of this behaviour in rails? regards. -a -- ==============================================================================| ara [dot] t [dot] howard [at] noaa [dot] gov | all happiness comes from the desire for others to be happy. all misery | comes from the desire for oneself to be happy. | -- bodhicaryavatara ===============================================================================
Erik Hatcher wrote:> > On Dec 7, 2005, at 10:01 AM, Jamis Buck wrote: > >> On Dec 7, 2005, at 7:59 AM, Erik Hatcher wrote: >> >>> >>> On Dec 7, 2005, at 9:12 AM, Nick Stuart wrote: >>> >>>> You would have to rename the attr to something like ''attr[]''. So now >>>> your request will look lik: >>>> >>>> /controller/action?attr[]=one&attr[]=two&attr[]=three >>>> >>>> This automatically turns it into an array ''attr'' which you can see in >>>> the debug out for the parameters. >>> >>> >>> Thanks for that tip. >>> >>> Though it doesn''t satisfy me :) >>> >>> The Java servlet API deals with this by allowing you to get a single >>> attribute or an array of attributes by a given name. It would be >>> good for Rails to support this multiple attribute case. For now, >>> I''ve numbered the params... attr0=one&attr1=two&attr2=three. >> >> >> Isn''t that what Nick just demonstrated? What about it is >> unsatisfactory? With >> >> /controller/action?attr[]=one&attr[]=two&attr[]=three >> >> You can do >> >> params[:attr] # => ["one", "two", "three"] > > > It''s unsatisfactory because it makes ugly URLs! :) > > There is really no technical reason the [] need to be on the attributes > - it only has to do with Rails squashing things into a Hash otherwise, > squeezing out all but the last attribute of a given name.You are right that there is no technical reason, but Rails wasn''t the first to do this - it''s how PHP does it (and DHH used PHP before devising Rails). See http://www.onlamp.com/pub/a/php/2003/03/13/php_foundations.html regards Justin
On Dec 7, 2005, at 11:10 PM, Justin Forder wrote:>>> /controller/action?attr[]=one&attr[]=two&attr[]=three >>> >>> You can do >>> >>> params[:attr] # => ["one", "two", "three"] >> It''s unsatisfactory because it makes ugly URLs! :) >> There is really no technical reason the [] need to be on the >> attributes - it only has to do with Rails squashing things into a >> Hash otherwise, squeezing out all but the last attribute of a >> given name. > > You are right that there is no technical reason, but Rails wasn''t > the first to do this - it''s how PHP does it (and DHH used PHP > before devising Rails). > > See http://www.onlamp.com/pub/a/php/2003/03/13/php_foundations.htmlThanks for the background. All I''m basically asking is there be some way to access the multiple parameters should (perhaps for legacy app integration reasons, or for the even more important aesthetic reasons) be accessible somehow. As it is, they are inaccessible without low-level hacking or scraping the request_uri manually again. I liked the proposal for params[:attr].list - that would be nice. Erik
Well, actually there is a way you access ''raw'' cgi parameters. Try request.params[''attr''] It will return an array of attr values in your case. Kent. On Thursday 08 December 2005 04:31, Erik Hatcher wrote:> > All I''m basically asking is there be some way to access the multiple > parameters should (perhaps for legacy app integration reasons, or for > the even more important aesthetic reasons) be accessible somehow. As > it is, they are inaccessible without low-level hacking or scraping > the request_uri manually again. > > I liked the proposal for params[:attr].list - that would be nice. > > Erik > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
ara.t.howard-32lpuo7BZBA@public.gmane.org
2005-Dec-08 15:10 UTC
Re: multiple same-named request parameters
On Thu, 8 Dec 2005, Erik Hatcher wrote:> > I liked the proposal for params[:attr].list - that would be nice.point me to the request_uri parsing code and i''ll code a patch - it would be only 20 lines of code or so. essentially module ObjectList def list @__list__ ||= [] end end ( request_key_values ).each do |k,v| if params.has_key? k params[k].extend ObjectList params[k].list << params[k] if params.list.empty? params[k].list << v else params[k] = v end end then you could do params["conditions"] # the first one params["conditions"].list # all of them totally backward compatible. i can find the parsing code myself - but i''m slammed today so if someone points me at it... -a -- ==============================================================================| ara [dot] t [dot] howard [at] noaa [dot] gov | all happiness comes from the desire for others to be happy. all misery | comes from the desire for oneself to be happy. | -- bodhicaryavatara ===============================================================================
On Dec 8, 2005, at 8:55 AM, Kent Sibilev wrote:> Well, actually there is a way you access ''raw'' cgi parameters. > Try > > request.params[''attr''] > > It will return an array of attr values in your case.Ah thank you! Where would I find this documented? I see that request is a ActionController::CgiRequest. That doesn''t appear listed at http:// api.rubyonrails.com and digging into the code for action_controller hasn''t shed light for me on this yet. I''m still coming to grips with Rails under the hood and the voodoo that it plays with Ruby. Many thanks for providing exactly what I was after. Erik> > Kent. > > On Thursday 08 December 2005 04:31, Erik Hatcher wrote: >> >> All I''m basically asking is there be some way to access the multiple >> parameters should (perhaps for legacy app integration reasons, or for >> the even more important aesthetic reasons) be accessible somehow. As >> it is, they are inaccessible without low-level hacking or scraping >> the request_uri manually again. >> >> I liked the proposal for params[:attr].list - that would be nice. >> >> Erik >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Well actually it''s quite simple. CgiRequest conveniently delegates to the instance of CGI object it encapsulates. So $ ri CGI Kent. On Thursday 08 December 2005 10:54, Erik Hatcher wrote:> On Dec 8, 2005, at 8:55 AM, Kent Sibilev wrote: > > Well, actually there is a way you access ''raw'' cgi parameters. > > Try > > > > request.params[''attr''] > > > > It will return an array of attr values in your case. > > Ah thank you! > > Where would I find this documented? I see that request is a > ActionController::CgiRequest. That doesn''t appear listed at http:// > api.rubyonrails.com and digging into the code for action_controller > hasn''t shed light for me on this yet. I''m still coming to grips with > Rails under the hood and the voodoo that it plays with Ruby. > > Many thanks for providing exactly what I was after. > > Erik > > > Kent. > > > > On Thursday 08 December 2005 04:31, Erik Hatcher wrote: > >> All I''m basically asking is there be some way to access the multiple > >> parameters should (perhaps for legacy app integration reasons, or for > >> the even more important aesthetic reasons) be accessible somehow. As > >> it is, they are inaccessible without low-level hacking or scraping > >> the request_uri manually again. > >> > >> I liked the proposal for params[:attr].list - that would be nice. > >> > >> Erik > >> > >> _______________________________________________ > >> Rails mailing list > >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Dec 8, 2005, at 11:02 AM, Kent Sibilev wrote:> Well actually it''s quite simple. CgiRequest conveniently delegates > to the > instance of CGI object it encapsulates. So > > $ ri CGIThanks again. And now that I look closer, I see good ol'' method_missing mojo: def method_missing(method_id, *arguments) @cgi.send(method_id, *arguments) rescue super end Thanks for removing my ignorance and showing me that I can actually have my cake and eat it too with respect to query parameters. Erik> > Kent. > > > On Thursday 08 December 2005 10:54, Erik Hatcher wrote: >> On Dec 8, 2005, at 8:55 AM, Kent Sibilev wrote: >>> Well, actually there is a way you access ''raw'' cgi parameters. >>> Try >>> >>> request.params[''attr''] >>> >>> It will return an array of attr values in your case. >> >> Ah thank you! >> >> Where would I find this documented? I see that request is a >> ActionController::CgiRequest. That doesn''t appear listed at http:// >> api.rubyonrails.com and digging into the code for action_controller >> hasn''t shed light for me on this yet. I''m still coming to grips with >> Rails under the hood and the voodoo that it plays with Ruby. >> >> Many thanks for providing exactly what I was after. >> >> Erik >> >>> Kent. >>> >>> On Thursday 08 December 2005 04:31, Erik Hatcher wrote: >>>> All I''m basically asking is there be some way to access the >>>> multiple >>>> parameters should (perhaps for legacy app integration reasons, >>>> or for >>>> the even more important aesthetic reasons) be accessible >>>> somehow. As >>>> it is, they are inaccessible without low-level hacking or scraping >>>> the request_uri manually again. >>>> >>>> I liked the proposal for params[:attr].list - that would be nice. >>>> >>>> Erik >>>> >>>> _______________________________________________ >>>> Rails mailing list >>>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails