Forum: Ruby on Rails multiple same-named request parameters

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.
erik (Guest)
on 2005-12-07 15:40
(Received via mailing list)
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
nicholas.stuart (Guest)
on 2005-12-07 16:12
(Received via mailing list)
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.
erik (Guest)
on 2005-12-07 17:01
(Received via mailing list)
On Dec 7, 2005, at 9:12 AM, Nick S. 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
jamis (Guest)
on 2005-12-07 17:05
(Received via mailing list)
On Dec 7, 2005, at 7:59 AM, Erik H. wrote:

> 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
mklame (Guest)
on 2005-12-07 17:14
(Received via mailing list)
Try /controller/action?attr[]=one&attr[]=two&attr[]=three
erik (Guest)
on 2005-12-07 17:14
(Received via mailing list)
On Dec 7, 2005, at 10:01 AM, Jamis B. wrote:

>>> see in
>
> 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 (Guest)
on 2005-12-07 18:35
(Received via mailing list)
On Wed, 7 Dec 2005, Nick S. 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
===============================================================================
justin (Guest)
on 2005-12-08 06:14
(Received via mailing list)
Erik H. wrote:
>>>>
>>> The Java servlet API deals with this by allowing you to get a  single
>> 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_fou...

regards

   Justin
erik (Guest)
on 2005-12-08 11:36
(Received via mailing list)
On Dec 7, 2005, at 11:10 PM, Justin F. wrote:
>
> 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_fou...

Thanks 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
ksruby (Guest)
on 2005-12-08 16:27
(Received via mailing list)
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.
ara.t.howard (Guest)
on 2005-12-08 17:13
(Received via mailing list)
On Thu, 8 Dec 2005, Erik H. 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
===============================================================================
erik (Guest)
on 2005-12-08 17:58
(Received via mailing list)
On Dec 8, 2005, at 8:55 AM, Kent S. 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
ksruby (Guest)
on 2005-12-08 18:02
(Received via mailing list)
Well actually it's quite simple. CgiRequest conveniently delegates to
the
instance of CGI object it encapsulates. So

$ ri CGI

Kent.
erik (Guest)
on 2005-12-08 18:11
(Received via mailing list)
On Dec 8, 2005, at 11:02 AM, Kent S. wrote:
> Well actually it's quite simple. CgiRequest conveniently delegates
> to the
> instance of CGI object it encapsulates. So
>
> $ ri CGI

Thanks 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
This topic is locked and can not be replied to.