Reputation: 4220
I would like to send a javascript array of arrays to my ruby controller. I am a bit lost. My question is in the controller. Here is what I have so far:
(totalChanges is an array of arrays. JSON.stringify(totalChanges) might look like this:
[[4,2,"","15"],[4,3,"","12"],[4,4,"","14"]]
app/views/index.html.erb:
<div id="dataTable" class="dataTable" style="width: 680px;height: 300px; overflow: scroll"></div>
<button>Save!</button>
<script>
var first = true;
var totalChanges = new Array();
$("#dataTable").handsontable({
//...some code that generates appropriate array totalChanges
});
var data = //..some code
$("#dataTable").handsontable("loadData", data);
$(function() {
$( "button").button();
$( "button" ).click(function() {
alert("clicked");
$.ajax({
type: "POST",
url: "/qtl_table/save",
data: {total_changes: JSON.stringify(totalChanges)},
success: function() { alert("Success!"); }
});
});
});
</script>
app/controllers/qtl_table_controller.rb:
def save
//Adding some things suggested by answers:
logger.debug "\n#{params[:total_changes].first}, #{params[:total_changes][1]}\n"
ar = JSON.parse(params[:total_changes])
end
I end up with these errors:
NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.first):
EDIT: I also had contentType: "application/json", and Accepts: "application/json" and when I took those out everything worked out. Thanks guys :)
Upvotes: 6
Views: 9650
Reputation: 64363
In javascript assign a root node for the JSON data
data: {qtls: totalChanges}
Now in the controller:
params[:qtls] # contains the Ruby array
params[:qtls].first # [4,2,"","15"]
Upvotes: 4
Reputation: 47618
JSON.parse
is your friend:
ar = JSON.parse(params[:total_changes])
#=> [[4, 2, "", "15"], [4, 3, "", "12"], [4, 4, "", "14"]]
Most likely you'll need to update your AJAX call to something like:
$.ajax({
type: "POST",
url: "/qtl_table/save",
data: { total_changes: JSON.stringify(totalChanges) },
success: function() { alert("Success!"); }
});
to give your array parameter total_changes
name.
Upvotes: 12
Reputation: 1137
The data being passed should be located somewhere in your params environment variable. It seems though that you don't know which environment variable it is in. If you know which one it is, that variable will holding the variable that you are passing, in your case the json string that is the array. Once you know how to access it, you can then update/create/save or what ever you need to do, and then do a redirect to your index:
redirect_to qtl_tables_path
Usually inspecting the params will show you how it is being passed if you don't know what the variable is called. When I want to know something like this and see everything that is being passed to the controller, I make it send it to a different controller action that has a view. In that view I will put something like:
<%= debug params %>
or
<%= inspect params %>
That way it prints out the params that are being passed, I can see where that variable is and how to access it in the params and then use it in the correct controller.
Does this make sense? The answers in this post should help you too:
Rails: How do I print the request parameters?
It has some nice examples of using your rails logger to print it as well.
I hope that is answer helps you out.
Upvotes: 1