Rubytastic
Rubytastic

Reputation: 15491

Simple? Post Jquery Array to rails controller#action

Cannot seems to figure out how I can post a query(json) array to a rails controller#action

I have just like

var myarray = []; ( with values )

My controller action I want to post to:

def process
end

Everywhere I find answers on how to get JSON -> Jquery But Ill need the other way around. Anyone knows how to do this? Can't be that hard?!

Upvotes: 1

Views: 1946

Answers (2)

Jakob W
Jakob W

Reputation: 3377

The jQuery documentation has information about posting: jQuery.post()

Does something like this help? $.post('path-to-process-action', {myarray: myarray})

Upvotes: 2

muffinista
muffinista

Reputation: 6736

There's probably a few ways to do this, but here's one. Use some JS like this to post to your controller:

var target = "your-action-url";
var myarray = [1,2,3,etc];
$.ajax({
    type: 'get',
    url: target + '?order='+myarray.join(',') ,
    dataType: 'script'
});

Then, in your controller:

data = params[:order].split(',')

Now you have an array that matches what you had in javascript.

Upvotes: 4

Related Questions