在rails中创建一个类似auto的建议搜索框

时间:2012-09-26 19:42:54

标签: jquery twitter autocomplete ruby-on-rails-3.2 autosuggest

请那些知道如何创建Twitter或Facebook的自动建议搜索框?我一直试图实现这一点......这是我从enter link description here得到的一些代码。

 <div class="search_index">


 <div id = "testsearch">
 <%=render :partial => 'search'%> // partial where the updated search elements will appear from 
 </div>

<%=form_tag(search_path,:method=>'get',:remote=>true) do%>

   <%=text_field_tag :search%>
   <%= submit_tag "search" ,:name=>nil%>

<%end%>

<%= set_focus_to_id 'search' %>  // I have a helper "set_focus_to_id"



<script> // small javascript file :)

 document.observe("dom:loaded", function() {   // ensures the page is loaded first
 new Form.Element.Observer(                    // Observes the text_field_tag every 2     seconds
  'search',
   2,
   respondToChange                         //refrences the function in the Layout      
   )                                         // on a change in search calls   respondToChange
  });

  </script>

  </div>

在我的标签上

 <script>
 function respondToChange() {
 $('search').up('form').submit()            // The ".up finds the form in the DOM"
 };
 </script>

代码似乎没有用,它甚至没有响应

请让任何人知道如何以更有效的方式实施此功能

我真的需要帮助

1 个答案:

答案 0 :(得分:2)

一个好的选择是使用预先存在的脚本:

  1. http://tweet.seaofclouds.com/
  2. http://jquery.malsup.com/twitter/
  3. 他们创造了自己的,并且它有一些值得考虑的功能。甚至使用。