Rubyコントローラーに配列のJavaScript配列を送信したいと思います。私は少し迷っています。私の質問はコントローラーにあります。これが私がこれまでに持っているものです:
(totalChanges は配列の配列です。JSON.stringify(totalChanges) は次のようになります。
[[4,2,"","15"],[4,3,"","12"],[4,4,"","14"]]
アプリ/ビュー/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
私はこれらのエラーで終わります:
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):
編集: contentType: "application/json" と Accepts: "application/json" もあり、それらを取り出したときにすべてがうまくいきました。みんなありがとう :)