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

ruby-on-rails – 如何将javascript对象的javascript值传递给ruby on rails中的控制器?,第1张

概述我有一个任务要在轨道上使用 ruby做knockout.js.我想将 javascript值发送给控制器. 我的index.html.erb是 <%= javascript_include_tag "knockout-2.2.0","country-state" %><table> <thead> <tr> <th>Country</th> 我有一个任务要在轨道上使用 ruby做knockout.Js.我想将 javascript值发送给控制器.
我的index.HTML.erb是

<%= 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,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 2013Processing by EmployeesController#show as JsON  Parameters: {"ID"=>"1","undefined"=>"undefined"}

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

解决方法 试试吧.

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

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

总结

以上是内存溢出为你收集整理的ruby-on-rails – 如何将javascript对象的javascript值传递给ruby on rails中的控制器?全部内容,希望文章能够帮你解决ruby-on-rails – 如何将javascript对象的javascript值传递给ruby on rails中的控制器?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1290125.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存