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?
3
  • 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 Commented Jul 1, 2014 at 14:23
  • BTW - In my case I do not use "nested" resources. Commented Jul 1, 2014 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 Commented Jul 1, 2014 at 14:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.