ruby-on-rails - 如何将 javascript 值作为 json 对象传递给 ruby​​ on rails 中的 Controller ?

标签 ruby-on-rails ruby json ruby-on-rails-3.2

我有一项任务是使用 ruby​​ on rails 来完成 knockout.js。我想将 javascript 值发送到 Controller 。 我的 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, 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对象发送到 Controller ?

最佳答案

试试这个。

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

           }
       });

您可以在 ajax 页面中使用变量 passval 来获取 dataTosave 的值,
变量 msg 将返回来自 ajax 的响应。

关于ruby-on-rails - 如何将 javascript 值作为 json 对象传递给 ruby​​ on rails 中的 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14432558/

相关文章:

python - 类型对象 'Users' 没有属性 '_meta'

ruby-on-rails - haml 条件 if/else 缩进

ruby-on-rails - 未定义的方法 : Activate Bin Path

ruby - 创建一系列数组元素的所有可能组合的字符串数组

ruby - 在图形 Ruby 上生成路径

ruby-on-rails - 基本的 Ruby/Rails 语法

mysql - 无法连接到 '127.0.0.1' 上的 MySQL 服务器

ruby-on-rails - 使用axlsx生成xls文件

c# - VS 2022 17.1.3 目前不会断点

javascript - redux reducer 在过滤状态时返回空数组