如何在Ruby on Rails中将javascript值作为json对象传递给控制器? [英] How to pass the javascript value as json object to the controller in ruby on rails?
本文介绍了如何在Ruby on Rails中将javascript值作为json对象传递给控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一项任务是在Rails上使用ruby来完成基因剔除.我想将javascript值发送到控制器. 我的index.html.erb是
I have got a task to do knockout.js using ruby on rails. I want to send the javascript value to the controller. My index.html.erb is
<%= javascript_include_tag "knockout-2.2.0","country-state" %>
<table>
<thead>
<tr>
<th>Country</th>
<th>State</th>
<th> </th>
</tr>
</thead>
<tbody data-bind='foreach: lines'>
<tr>
<td>
<select data-bind='options: sampleProductCategories, optionsText: "country", optionsCaption: "Select...", value: category'> </select>
</td>
<td data-bind="with: category">
<select data-bind='options: products, optionsText: "country", optionsCaption: "Select...", value: $parent.product'> </select>
</td>
<td>
<a href='#' data-bind='click: $parent.removeLine'>Remove</a>
</td>
</tr>
</tbody>
</table>
<button data-bind='click: addLine'>Add</button>
<button data-bind='click: save'>Submit</button>
<script>
$(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/<%=@employee.id%>',
dataType: 'json',
async: false,
method:'PUT',
data:dataToSave,
success: function(data) {
}
});
};
};
ko.applyBindings(new Cart());
});
</script>
在终端中,它显示为
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对象发送到控制器?
How to send the selected state and country to the controller as json object?
推荐答案
只需尝试一下.
$.ajax({
url:'/employees/<%=@employee.id%>',
dataType: 'json',
data: { passval: dataToSave},
success: function(msg)
{
}
});
您可以在ajax页面中使用变量passval
来检索dataTosave
,
的值
并且变量msg
将返回来自ajax的响应.
you can use the variable passval
in the ajax page for retrieving the value of dataTosave
,
and the variable msg
will return the response from the ajax.
这篇关于如何在Ruby on Rails中将javascript值作为json对象传递给控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文