Form parameter naming convention in Rails

Reading time ~1 minute

      = text_field_tag "queue_items[][position]", queue_item.position
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"8pWUHvTO4DlVjHuINLlxy8thIn8v8Jm5CfiYYvb1XYQ=", "queue_items"=>[{"position"=>"1"}], "commit"=>"Update Instant Queue"}

In the rails guide:

Combining Them We can mix and match these two concepts. One element of a hash might be an array as in the previous example, or you can have an array of hashes. For example, a form might let you create any number of addresses by repeating the following form fragment

  <input name="addresses[][line1]" type="text"/>
  <input name="addresses[][line2]" type="text"/>
  <input name="addresses[][city]" type="text"/>

This would result in params[:addresses] being an array of hashes with keys line1, line2 and city. Rails decides to start accumulating values in a new hash whenever it encounters an input name that already exists in the current hash.

There’s a restriction, however, while hashes can be nested arbitrarily, only one level of “arrayness” is allowed. Arrays can usually be replaced by hashes; for example, instead of having an array of model objects, one can have a hash of model objects keyed by their id, an array index or some other parameter.

to_param in Ruby on Rails

If I want a custom slug=======================I walk through this cutstom slug.1. create migration `add_slug` to add a column inside the...… Continue reading

What is ORM in Rails mean?

Published on July 14, 2017