1

I am using Ruby on Rails 4.1 and I would like to know some more about "indexes" added to parameters when incoming data contains array of hashes.

That is, for instance, when I run the following code (as an example, the code executes a simple AJAX request to my application)

var my_data = [
  { "a1": 1, "b1": 2 },
  { "a2": 1, "b2": 2 },
  { "a3": 1, "b3": 2 },
];

$.ajax({
  type: "POST",
  url:  "http://0.0.0.0:3000/path.json",
  data: { "my_data": my_data }
});

Then Rails parses the following parameters by "automagically" adding "0", "1", "2" indexes:

{"my_data"=>{"0"=>{"a1"=>"1", "b1"=>"2"}, {"1"=>{"a2"=>"1", "b2"=>"2"}, {"2"=>{"a3"=>"1", "b3"=>"2"}}
  1. Why Rails adds those indexes?
  2. When Rails is supposed to add indexes? And when not?
  3. How to avoid Rails to add indexes?
user502052
  • 14,803
  • 30
  • 109
  • 188
  • I think this is coming from having 'nested' parameters - you'll probably have to use the `child_index` option - not sure if this is exactly the same, but take a look [here](http://stackoverflow.com/a/20441143/2128691) – dax Jul 01 '14 at 14:23
  • BTW - In my case I do not use "nested" resources. – user502052 Jul 01 '14 at 14:28
  • right, sorry, i didn't mean the gem, just that this is how rails deals with params that have a hierarchical structure – dax Jul 01 '14 at 14:32

0 Answers0