The Nested Ruby Params Hash for Complex HTML Forms and Sinatra

Flatiron School / 2 July 2013

The following is a guest post by Alex Au and originally appeared on his blog. Alex is currently a student at The Flatiron School. You can follow him on Twitter here.

Showcasing the Normal, Unaltered Use of the Params Hash

The params hash is a “rack” object that is accessible through Sinatra’s request object. This params hash stores information posted by html forms in the form of -you guessed it – a hash, which contains a key-value pair. The key of this pair defaults to the value of the <input>’s name=“attribute”. Meanwhile, the info that the user submits is the value of that key-value pair.

For the above html, you can access the user’s entry in app.rb within the post '/' do route:

If I entered “New York City” into the <input name="location"/>, then @location = "New York City". Similarly, if there was another input, <input name="arrival_date">, where I entered “June,” then the params hash would have two key-value pairs.

Kick it up a Notch with a Nested Params Hash

Amazingly, you can modfiy the data structure of the params hash by carefully altering the name attribute of <input>. For example, you can make the params hash contain an array of hashes. Because this process is slightly complex, I will first show off what’s possible with an example, then list further below the key points for understanding how to do it.

The above form outputs the following params hash:

As you can see, the params hash’s top-layer has only one key-value pair, with “game” as its key. If you look at the html form, this initially makes sense because game is repeated as the top-layer for all three inputs.

As programmers, we then have a choice of modifying the value of this pair from a string into something else. If we want to turn the pair’s value into an array, then we write an empty array as above. If we want to make the value a hash, then we surround the attribute in square brackets.

Notice that each hash in the array contains 3 key-value pairs. Just like the first example with “arrival_date” and “location,” adding a key-value pair on the same layer will add a key-value pair to that same hash – not create another hash inside a hash.

At this point, your brain might be wheeling a bit. Hopefully these rules below will help you clarify how to alter the params hash through html forms.

Primer for Understanding Params

  1. Realize that because the params hash stores form data, the user’s entry should always be the value of a key-value pair.

  2. If you think of params hash as having a pointer to what will be returned, then everytime you add an extra layer, the data type of the extra layer – be it array or hash, will be the return of the formerly-lowest layer.

    • In other words, <input name="game[][name][]" /> sets the value of the :name=>value pair to an array, and although this wouldn’t break your app, it doesn’t make that much sense because there would be no way to track the attributes of a particular object. Every entry would be added to the array, rather than the array containing hash elements.

    • Therefore you must end it with a key-value pair like <input name="game[][name][][syllables]" />. Apologies for the quirky example, but here the user would input the number of syllables in the game’s name, of which there are many: such as “ping pong,” “table tennis,” and “pong.”

  3. By convention, you leave the top-layer of the params hash as being without square brackets, despite the fact that it is a key-value pair inside the params hash.

  4. When in doubt, practice using the following method in irb:

and use the following syntax: p("game[][name][][syllables]" )

Now as a challenge, can you decipher what the above params hash looks like?

Answer: params == {:game => [{:name=> [ {:syllables=> value} ] }] }

Sugar in Your Coffee Previous Post Traditional Economics vs. Seth Godin's Economics Next Post