0

What I want to accomplish is to pass a 2D array from javascript to something that ruby will recognize. All objects are strings

I have been using gon-sinatra, but it doesn't do exactly what I need. I can pass store the string I want to pass as gon.text doing

@temp = gon.text
array.push(@temp)

This doesn't work because it shows gon.text as a nil object type, when I want it as a string. gon.text.to_s returns an empty string, so when I try to display it, nothing happens. alert("<%= @temp %>") // displays an empty string

I'm at a bit of a loss here and don't know the best way to approach this. Would it be better to store the array as a json, and then read in the json using ruby instead?

0

2 Answers 2

2

Yes. Convert your array to json(a string) with js:

var data = JSON.stringify(yourArray);

Send the string to your ruby script. Then parse the string into an Array with ruby:

require 'json'

arr = JSON.parse(the_string)
Sign up to request clarification or add additional context in comments.

Comments

1

In Javascript you do something like the following:

var myArray = [ ['str1','str2'], ['str3','str4'] ];
$.get('/endpoint', { myArray: myArray })

And your endpoint with sinatra would be:

get 'endpoint' do
    myArrayStr = params[:myArray]
    error!('invalid array') unless myArrayStr
    @myArray = JSON.parse(myArrayStr)
    gon.rabl 'goners/yourFile.rabl', :instance => self
end

And in your gon file you would reference this with:

alert(gon.myArray[0][0]); // str1

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.