1

So im getting an array from parameters(JS)

"[[\"01000290056001\",0],[\"01000290056002\",0]]"

The problem is in quotes that are at the start and at the end, ruby reads this not as array but as string.

Here is the screenshot with parametres that I get from JS (http://prntscr.com/p8fagj)

JS that is used

$('.button').bind("click", function() {
        var ar_id = $(this).attr("testval");
        collectID = $('input[checked]:not(:checked), input:not([checked]):checked')
            .map( ( _, it ) => [[ it.value, it.checked ? 1 : 0 ]] )
            .get()

        $.get('/places/call_this',
            {
                cad_ids: JSON.stringify(collectID),
                ar_id: ar_id

            },
            function(response) {
                // nothing here
            }
        );
    })

The question is how to correctly pass the array to Ruby Controller without quotes? Or how to remove them in the controller directly?

So in the end, I need something like this

[["01000290056001",0],["01000290056002",0]]

It will be used for .each function.

1
  • Typically you should not pass json as a get parameter, but in the body either on it's own, or as part of a post query string Commented Sep 19, 2019 at 21:13

1 Answer 1

1

You can use JSON.parse:

require 'json'

response = "[[\"01000290056001\",0],[\"01000290056002\",0]]"
parsed_response = JSON.parse(response)
# => [["01000290056001", 0], ["01000290056002", 0]]

parsed_response.each do |item| 
  # whatever you want
end

That will convert your response into a Ruby array for you to use. Let me know how you get on or if you have any questions.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, thank you! It worked. Have tried it before and were unlucky :(.
Great, glad it helped @Marshmello - if you're satisfied it answered the question please feel free to give it a tick confirming that :)
Sure! Then you one more time :)

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.