简体   繁体   中英

how to implement ajax in Ruby on rails via jquery

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:

    $("#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?

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, eg, no manipulation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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