What is the simplest way of getting the nth User in a database (User.find(n)
in rails) using a field (where n is the input) and a button, and displaying it on the page using Ajax?
<input id="search_input" type="text">
<button id="search_button">Search</button>
<div id="output> Output displayed here </div>
I read the Ajax tutorial on W3Schools where an XMLHttpRequest object is created, but this seems way too long winded for doing something so simple. Is there an easier way in rails?
This is the Javascript function I would have to call when the button is pressed
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();
}
and then in file ajax_info.rb (which has to be routed correctly) have the rails code.
I tried searching for Ajax on Rails but all I find is stuff to do with forms_for which I don't think are applicable here. I need to function to execute on a button press.