2013-04-04 54 views
0

什么是使用场(其中n是输入)和一个按钮获得的第n个用户在数据库(User.find(n)中轨),并使用Ajax在页面上展示它的最简单的方法?使用Ajax on Rails的搜索

<input id="search_input" type="text"> 
<button id="search_button">Search</button> 
<div id="output> Output displayed here </div> 

我看了其中创建XMLHttpRequest对象的Ajax tutorial on W3Schools,但这似乎太长篇大论做这么简单的东西。在rails中有更简单的方法吗?

这是JavaScript函数我会当按钮被按下

function find_nth_user() { 
var xmlhttp; 
if (window.XMLHttpRequest) 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("output").innerHTML=xmlhttp.responseText; 
    } 
    } 
xmlhttp.open("GET","ajax_info.rb",true); 
xmlhttp.send(); 
} 

,然后在文件ajax_info.rb(它必须被正确地路由)来调用具有Rails代码。

我试图寻找适合于Ajax on Rails的,但所有我觉得是东西做forms_for我不认为是适用在这里。我需要运行按钮按下执行。

回答