Multiple same-named request parameters

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

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 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 :slight_smile:

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

Try /controller/action?attr[]=one&attr[]=two&attr[]=three

On Dec 7, 2005, at 7:59 AM, Erik H. wrote:

Thanks for that tip.

Though it doesn’t satisfy me :slight_smile:

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

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! :slight_smile:

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

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

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_foundations.html

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

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! :slight_smile:

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 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

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.

Well actually it’s quite simple. CgiRequest conveniently delegates to
the
instance of CGI object it encapsulates. So

$ ri CGI

Kent.

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

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