0

User.find(n)使用字段(其中 n 是输入)和按钮获取数据库(在 rails 中)中的第 n 个用户并使用 Ajax 将其显示在页面上的最简单方法是什么?

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

我阅读了W3Schools 上的 Ajax 教程,其中创建了 XMLHttpRequest 对象,但这对于做如此简单的事情来说似乎太冗长了。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 代码。

我尝试在 Rails 上搜索 Ajax,但我发现的只是与 forms_for 相关的东西,我认为这些东西在这里不适用。我需要在按下按钮时执行。

4

1 回答 1

0

您不应该像这样处理准系统对象。jQuery/Prototype/Zepto 存在,所以你不必做这些事情。

观看有关使用 AJAX 搜索的 railscast:http ://railscasts.com/episodes/240-search-sort-paginate-with-ajax

于 2013-04-04T17:10:07.847 回答