1

I am assembling data into an array for an ajax call to a Rails controller action. Unfortunately, I don't know how to append the values of this array onto my query string so that they will end up in the params object inside my controller action.

The array is a simple array of strings.

my_ids = ["1","2","3"]

My main query string is a serialized form.

I want to add this so that in my controller action:

params[:my_ids] == ["1","2","3"]

What can I do?

I am using Rails 3.2 and jQuery.

1 Answer 1

1

Try:

$.ajax({
        url: '/controller/action',
        type: 'POST',
        dataType: "json",
        data: { 
            my_ids: JSON.stringify(my_ids)
        },
        success: function () { alert('success'); },
        error: function (event, request, settings) {  
            alert('Error' + ' : ' + settings); }
});
Sign up to request clarification or add additional context in comments.

1 Comment

You'll also need my_ids = JSON.parse(params[:my_ids]) in your controller.

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.