如何将javascript对象的javascript值传递给ruby on rails中的控制器?

我有一个任务要在轨道上使用ruby做knockout.js。 我想将javascript值发送给控制器。 我的index.html.erb是

 
Country State
Remove
$(document).ready(function() { function formatCurrency(value) { return "$" + value.toFixed(2); } var CartLine = function() { var self = this; self.category = ko.observable(); self.product = ko.observable(); self.subtotal = ko.computed(function() { return self.product() ? self.product().price * parseInt("0" + self.quantity(), 10) : 0; }); // Whenever the category changes, reset the product selection self.category.subscribe(function() { self.product(undefined); }); }; var Cart = function() { // Stores an array of lines, and from these, can work out the grandTotal var self = this; self.lines = ko.observableArray([new CartLine()]); // Put one line in by default self.grandTotal = ko.computed(function() { var total = 0; $.each(self.lines(), function() { total += this.subtotal() }) return total; }); // Operations self.addLine = function() { self.lines.push(new CartLine()) }; self.removeLine = function(line) { self.lines.remove(line) }; self.save = function() { var dataToSave = $.map(self.lines(), function(line) { return line.product() ? { state: line.product().country } : undefined }); alert("Could now send this to server: " + JSON.stringify(dataToSave)); $.ajax({ url: '/employees/', dataType: 'json', async: false, method:'PUT', data:dataToSave, success: function(data) { } }); }; }; ko.applyBindings(new Cart()); });

它在终端显示如

 Started GET "/employees/1?undefined=undefined" for 127.0.0.1 at Mon Jan 21 13:36:15 +0530 2013 Processing by EmployeesController#show as JSON Parameters: {"id"=>"1", "undefined"=>"undefined"} 

如何将选定的州和国家作为json对象发送到控制器?

试试吧。

  $.ajax({ url:'/employees/<%=@employee.id%>', dataType: 'json', data: { passval: dataToSave}, success: function(msg) { } }); 

你可以使用ajax页面中的变量passval来检索dataTosave的值,
变量msg将返回ajax的响应。

任何ajax http调用都可以。

 $.ajax({ url: myUrl, dataType: 'json', async: false, data: myData, success: function(data) { //stuff } }); 

要么

  $.getJSON(muUrl, myData, function(data) { //stuff });