简体   繁体   English

Ruby on Rails的表格搜索字段

[英]Search Field for Table with Ruby on Rails

I'm fairly new to Rails, but have an extensive Java/C++ background and am working on a project where I display a search box and a table using the following html: 我对Rails相当陌生,但是具有广泛的Java / C ++背景,并且正在研究一个使用以下html显示搜索框和表格的项目:

<h2>Search ID</h2>
  <%= form_tag do %>
  <%= label_tag :user_id, "User ID" %>
  <%= text_field_tag :user_id %>
  <%= submit_tag "Search" %>
  <% end %>
<h2>List of People</h2>
<div>
  <table id="people_list" class="tablesorter">
    <thead>
    <tr>
      <th>User Name</th>
      <th>ID Number</th>
    </tr>
    </thead>
    <tbody>
      <% Users.each do |user| %>
        <tr><td><%= user.name %></td><td><%= user.id_number %></td></tr>
      <% end %>
  </table>
</div>

What I want is a search box that will allow you to essentially "query" the table with a user_id and give back the user. 我想要的是一个搜索框,它使您可以使用user_id从本质上“查询”表并退还用户。 Ideally I would like to do this without redirecting to a new page when the search button is pressed. 理想情况下,当我按下搜索按钮时,我希望不重定向到新页面即可执行此操作。

Is it possible to dynamically show a row of the table underneath the search bar and above the table if a search is done? 如果完成搜索,是否可以在搜索栏下方和表格上方动态显示表格的一行? If so, how would I do this in the html.erb file? 如果是这样,我将如何在html.erb文件中执行此操作? What method would the search button go to in my controller? 搜索按钮将在我的控制器中转到哪种方法? I imagine it would be different than the method that my table uses to populate itself. 我想这将与我的表用来填充自身的方法不同。

Any help would be greatly appreciated. 任何帮助将不胜感激。

I haven't used any but have heard about these. 我什么都没用,但听说过。

elasticsearch and thinking-sphinx 弹性搜索思想狮身人面像

or (a thought) U could make ajax calls and return the result through json object. 或者(可以想到)U可以进行ajax调用并通过json对象返回结果。

If you can't find an ajax way of doing it, this method renders the page again with the search criteria included. 如果找不到这样做的ajax方法,则此方法将再次显示包含搜索条件的页面。 There is a RailsCast - Simple Search that runs through a simple search form that is included at the top of the page. 有一个RailsCast-简单搜索,它通过页面顶部包含的简单搜索表单运行。 Otherwise, there is gem called Ransack that allows a bit more complexity for search forms. 否则,有一个叫做Ransack的宝石,它会使搜索表单更加复杂。

Hopefully these resources are of some use. 希望这些资源有一定用处。

http://railscasts.com/episodes/37-simple-search-form http://railscasts.com/episodes/37-simple-search-form

http://railscasts.com/episodes/370-ransack http://railscasts.com/episodes/370-ransack

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM