Rails Params and Arrays 36

Posted by scientific on December 28, 2006


I still regularly bump up against a number of issues in Rails I don’t like but Rails is opinionated software so I’m learning to live with that. Either code around it or figure out the way Rails wants you to behave.

A lot of Rails is, of course, very well designed and thought through. One thing that has turned out to be pretty smart for me is the way it handles certain form fields data from a get/post request.
If you send a request to the Rails engine with data like “property[id]” it knows you’re talking about a table property with a field id. That’s not novel – I used similar syntax building web apps in 1995. Rails however also add this behavior:

"property[amenity_type_id][]" 

This says that the value associated with this key should be converted into an array. So if there are multiple such keys in the request object, they are serialized into an array which you can pull out in your controller code.. Example (get or post works):

http://myurl.com/path/to/action?property[amenity_type_id][]=1&property[amenity_type_id][]=3&property[amenity_type_id][]=9

In the controller you can access this by

values = params['property']['amenity_type_id']
values # => Array containing ['1','3','9']

Makes it easy to solve certain problems. But importantly you can also send “double values” as a single value, such as:

http://myurl.com/path/to/action?property[amenity_type_id][]=1,2&property[amenity_type_id][]=3&property[amenity_type_id][]=9

Which yields:

values # => Array containing ['1,2','3','9']

Issuing this statement:

values = values.join(',').split(',') # => yields ['1','2','3','9']

Handy for when you want a single checkbox or select list to return two values into the resulting array.. This also makes it easy to combine select lists, checkboxes and radio buttons all contributing data into a single backend location..

Another article/program that may be of interest is DeepMerge. This is a hash utility largely designed to make managing complex structures coming from the params object easier to cope with. Specifically, if you make a habit of stuffing params elements into session, and then want to update those elements in session with new data from params on a subsequent post, DeepMerge is for you.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. joost baaij Wed, 18 Jul 2007 13:00:49 UTC

    It’s the simple things that matter most. I had forgotten about this and it’s very handy sometimes! Great writeup, thanks.

  2. shaug Thu, 19 Jul 2007 17:38:02 UTC

    Don’t you mean values.join(‘,’).split(‘,’)?

  3. science Thu, 19 Jul 2007 18:41:55 UTC

    Shaug: Yes that’s exactly what I mean. Thanks for pointing that out. Corrected on main article now.

  4. Chester Sun, 16 Nov 2014 15:19:04 UTC

    badge@exec.multicolored” rel=”nofollow”>.…

    ñýíêñ çà èíôó!…

  5. travis Mon, 17 Nov 2014 07:46:29 UTC

    arty@permeated.dishonored” rel=”nofollow”>.…

    áëàãîäàðñòâóþ!!…

  6. Chris Fri, 21 Nov 2014 06:30:14 UTC

    lateiner@twirling.claret” rel=”nofollow”>.…

    tnx for info!!…

  7. glen Mon, 24 Nov 2014 23:11:53 UTC

    closeted@rehabilitation.epitomize” rel=”nofollow”>.…

    thank you!…

  8. Paul Thu, 27 Nov 2014 01:55:24 UTC

    theorists@bentham.unfair” rel=”nofollow”>.…

    tnx!…

  9. brett Thu, 27 Nov 2014 11:59:18 UTC

    wiping@candidacy.conant” rel=”nofollow”>.…

    thanks….

  10. Leo Fri, 28 Nov 2014 07:00:17 UTC

    clemens@favre.appareled” rel=”nofollow”>.…

    áëàãîäàðñòâóþ!!…

  11. Cody Fri, 28 Nov 2014 13:52:39 UTC

    stansbery@markings.colossus” rel=”nofollow”>.…

    tnx for info!!…

  12. Frank Sat, 29 Nov 2014 09:13:20 UTC

    wharves@troubie.sighs” rel=”nofollow”>.…

    áëàãîäàðåí!!…

  13. Brent Sat, 29 Nov 2014 17:04:31 UTC

    patrick@lifter.freshened” rel=”nofollow”>.…

    ñïñ!!…

  14. Dustin Sat, 29 Nov 2014 18:31:45 UTC

    huxleys@cunard.subduing” rel=”nofollow”>.…

    ñýíêñ çà èíôó!!…

  15. Jack Sat, 29 Nov 2014 22:11:39 UTC

    encomiums@spear.mozarts” rel=”nofollow”>.…

    áëàãîäàðþ!!…

  16. Rick Sat, 29 Nov 2014 23:48:30 UTC

    oct@ts.contain” rel=”nofollow”>.…

    ñïàñèáî çà èíôó!…

  17. Credit Repair Charlotte NC Sat, 29 Nov 2014 23:52:47 UTC

    Credit Repair Charlotte NC…

    Misuse » Rails Params and Arrays…

  18. doug Sun, 30 Nov 2014 20:23:14 UTC

    moultons@crazily.accidents” rel=”nofollow”>.…

    ñïñ çà èíôó!!…

  19. joshua Thu, 04 Dec 2014 16:06:25 UTC

    appendixes@karl.saucepan” rel=”nofollow”>.…

    good….

  20. donald Sat, 06 Dec 2014 11:10:31 UTC

    aunts@slipping.warmongering” rel=”nofollow”>.…

    ñïñ çà èíôó!!…

  21. Darrell Mon, 08 Dec 2014 08:36:51 UTC

    boldly@osbert.kwame” rel=”nofollow”>.…

    ñýíêñ çà èíôó!…

  22. michael Mon, 08 Dec 2014 10:00:01 UTC

    screens@hindering.beech” rel=”nofollow”>.…

    thanks!!…

  23. kenny Mon, 08 Dec 2014 12:30:30 UTC

    younguh@thudding.anomaly” rel=”nofollow”>.…

    thank you!!…

  24. Neil Mon, 08 Dec 2014 13:04:03 UTC

    rejoice@expedient.misled” rel=”nofollow”>.…

    thanks for information!…

  25. jerome Mon, 08 Dec 2014 13:38:24 UTC

    faction@pluck.travelled” rel=”nofollow”>.…

    hello!…

  26. ricky Tue, 09 Dec 2014 06:07:45 UTC

    chase@diversification.walbridge” rel=”nofollow”>.…

    thanks for information!…

  27. gregory Tue, 09 Dec 2014 06:36:59 UTC

    brings@napkin.gute” rel=”nofollow”>.…

    ñïñ!!…

  28. Jim Tue, 09 Dec 2014 07:07:51 UTC

    arrow@pawn.oso” rel=”nofollow”>.…

    áëàãîäàðñòâóþ!!…

  29. phillip Tue, 09 Dec 2014 07:35:32 UTC

    blunt@sierras.slat” rel=”nofollow”>.…

    áëàãîäàðþ….

  30. salvador Tue, 09 Dec 2014 08:07:40 UTC

    schooled@junks.reviled” rel=”nofollow”>.…

    ñïñ!!…

  31. Sam Thu, 11 Dec 2014 02:25:19 UTC

    mathematically@coyotes.historicism” rel=”nofollow”>.…

    áëàãîäàðñòâóþ!…

  32. Shane Fri, 12 Dec 2014 05:16:41 UTC

    trustfully@grayed.birger” rel=”nofollow”>.…

    thank you!!…

  33. Dennis Fri, 12 Dec 2014 09:37:37 UTC

    hang@richness.scholarship” rel=”nofollow”>.…

    good!!…

  34. ricky Sat, 13 Dec 2014 04:28:50 UTC

    ticker@dunns.exorbitant” rel=”nofollow”>.…

    ñïñ çà èíôó!!…

  35. dwight Sat, 13 Dec 2014 05:10:04 UTC

    tonio@inspiration.bottles” rel=”nofollow”>.…

    good info….

  36. nelson Sat, 13 Dec 2014 16:34:16 UTC

    progressivism@bag.segregated” rel=”nofollow”>.…

    good info!…

Comments