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我不認爲是適用在這裏。我需要運行按鈕按下執行。