RubyRedGrapefruit
RubyRedGrapefruit

Reputation: 12214

How can I make an Javascript array into a query string that will be read into Rails controller action params?

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.

Upvotes: 1

Views: 201

Answers (1)

mccannf
mccannf

Reputation: 16659

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); }
});

Upvotes: 1

Related Questions