如何通过jquery在rails上实现Ruby中的ajax

时间:2021-07-21 14:29:56

How do I pass a few of my form field(s) values to a controller usnig ajax/jquery? For example, in php/jquery I do something like this:

如何将一些表单字段值传递给控制器​​usnig ajax / jquery?例如,在php / jquery中我做了这样的事情:

    $("#test-btn").click(function(){
     var name=$("#name").val();
     var age=$("#age").val();
     $.post("insert.php",{name: name,age: age},
      function(data){
       $("#respone").html(data).hide().fadeIn(500);
      });
    });


//insert.php
<?php
 //insert values to database!
?>

How do I acheive a similar functionality in rails using ajax/jquery?

如何使用ajax / jquery在rails中实现类似的功能?

1 个解决方案

#1


0  

Depends.

You can do it the exact same way, which is suitable if you need to manipulate any data.

你可以用完全相同的方式完成它,如果你需要操作任何数据,这是合适的。

You can use Rails' UJS's :remote => true flag and do nothing; here's a quick blog post on it. This is suitable for when you don't need to do anything other than submit, e.g., no manipulation.

你可以使用Rails的UJS:remote => true flag并且什么都不做;这是一篇关于它的快速博客文章。这适用于除了提交之外不需要做任何事情,例如,没有操纵。

#1


0  

Depends.

You can do it the exact same way, which is suitable if you need to manipulate any data.

你可以用完全相同的方式完成它,如果你需要操作任何数据,这是合适的。

You can use Rails' UJS's :remote => true flag and do nothing; here's a quick blog post on it. This is suitable for when you don't need to do anything other than submit, e.g., no manipulation.

你可以使用Rails的UJS:remote => true flag并且什么都不做;这是一篇关于它的快速博客文章。这适用于除了提交之外不需要做任何事情,例如,没有操纵。