Alexey
Alexey

Reputation: 3637

Ajax to Rails through jQuery arrays

I'm passing an array of arrays from jQuery to Rails through Ajax:

search_array = [arr1, arr2];

In the console, it shows it to me as this is being passed:

search_array => {"0" => ["Trader"], "1" => ["x1"]}

Which is correct.

How do I access the values of each array? I am able to access just arrays of values through normal declaration:

myArrayInRails = params[:searchArray]

and then do normal calls such as:

myVar = myArrayInRails[0] 

And get the value, but I can't access deeper in with:

myVar = myArrayInRails[0][0]

It gives me:

Undefined method '[]' for nil:nilClass.

Upvotes: 1

Views: 71

Answers (1)

cicloon
cicloon

Reputation: 1099

What you have there is a hash so you shouldn't be accessing it through indexes. Doing it through keys will work, p.e.:

params[:search_array]['1'] 

Or

params[:search_array][:1]

Upvotes: 1

Related Questions