简体   繁体   English

Rails:使用Jquery发送Ajax对象并将数据类型保持在Rails上

[英]Rails: sending ajax object using Jquery and keeping datatype on rails

I'm using Jquery for creating object and sending to server: 我正在使用Jquery创建对象并将其发送到服务器:

    data = {};
    data.number = 5;
    $.ajax({
        url: 'some_link',
        type: 'POST',
        dataType: 'json',
        data: data,
        success: function() {
            alert('data sent');
        },
        error: function() {
            alert('fail');
        }
    });

And on rails, I test submitted param by: 在轨道上,我通过以下方式测试提交的参数:

params[:number] => "5"

How can I force rails to understand 5 instead of "5". 我怎样才能迫使rails理解5而不是“ 5”。 Because I send a large bunch of data, I cannot manually do conversion. 因为我发送了大量数据,所以无法手动进行转换。

Thanks 谢谢

If you need to save to a database, then your model should have the attribute type set as integer . 如果需要保存到数据库,则模型应该将属性类型设置为integer When you save it, it will automatically coerce the string '5' into the integer 5 保存时,它将自动将字符串'5'强制转换为整数5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM