request parsing and params hash

Jonathan Rochkind wrote:

If I submit a request that ends in:

?foo=bar&foo=baz

Then my assumption would be that params[:foo] would have an array in it: ['bar', 'baz']. But it doesn't. It only has the first value, 'bar', and the 'baz' is apparently completely ignored.

Is there any way to fix this?

Rails uses PHP's array notation so you want your URL to be:

?foo=bar&foo=baz

In this case params[:foo] will be ['bar', 'baz']

Eric

Jonathan Rochkind wrote:

Hmm, thanks. I ran into that with PHP, and I was hoping it would be solved in Rails!

I'm not sure it is a problem to be solved. Perl's CGI library works like you indicate and the problem with functionality like that is you never know what you are getting back. Are you getting a scalar, array, hash, what? So if the user checks only one checkbox you will get an scalar but if they check two you have to deal with a hash. Then you code get's ugly with things like:

foo = [params[:foo]].flatten

Just to get a simple set of values from a checkbox. The PHP/Rails way is better because the form is specifying "I am going to send you an array, even if it is just one value".

The PHP/Rails method also kicks butt because you can send complex data structures. Want to update a user object and account object each with their own (possibly overlapping attributes). Easy with the Rails/PHP method:

User

But I’m getting rather frustrated with the typical Rails community response when someone

brings up something they need to do that Rails doesn’t do well: “Well, don’t do that, it’s better to do this.” When, as in this case, the thing to be done is in fact a perfectly reasonable thing, and is

additionally constrained by outside forces not under the control of the developer.

I take issue with this comment. The whole community is based on people who volunteer help in their free time. You are free to take the advice that people give you and do what you will with it, but it’s not a good idea to blame Rails or the community because you have an edge case that the framework was not designed to handle. It’s true that we tend to try to steer new developers to a preferred method of doing things because you can’t always do things the way you did them in the language you came from. Rails is opinionated… it relies on you doing things its way and if you choose to ignore that, you’re kinda on your own.

But fear not… Rails is open-source. You can submit patches or create a plugin to extend / modify how it works. The core team made that possible and really easy. So, the developer is indeed in full control of all of the framework and is not constrained by the framework. It’s the old 80/20 rule, or at least in a lot of cases, 95/5.

I understand your frustration, but I imagine that you will indeed need to parse the parameters yourself. No need to override how Rails parameter parsing works, just roll your own parser for that specific piece and call it instead of params.

I’m sorry you’re frustrated by the answers you’re receiving, but as you learn more about Rails, you’ll discover why you get the answers you get.

Hope things work out for you! Have a great day.