简体   繁体   English

Ruby on Rails:如何实现搜索功能

[英]Ruby on Rails: How to implement search function

I'm very new to Ruby on Rails and actually, I'm trying to implement a search function into my Ruby on Rails page. 我是Ruby on Rails的新手,实际上,我正在尝试在我的Ruby on Rails页面中实现搜索功能。 It's a page where you can add users to a database, you can also edit or delete them. 这是一个页面,您可以在其中添加用户到数据库,也可以编辑或删除用户。 There is my index page where all of those users are getting listed, and, above this table, there is a search function (well, it's just a text field and a button). 我的索引页面上列出了所有这些用户,在此表上方,有一个搜索功能(好吧,它只是一个文本字段和一个按钮)。

What I want to do it now: I want to implement a search function which is searching for the surname, room, key, manager and so on. 我现在要执行的操作:我想实现一个搜索功能,该功能正在搜索姓氏,房间,钥匙,管理员等。 I already tried some things but I finally don't know how to implement a search function in general (what should I write, where should I write it? I heard from models and controllers, but anyway I'm not 100% sure). 我已经尝试过一些方法,但是最终我还是不知道如何实现搜索功能(我应该写什么,应该在哪里写?我从模型和控制器那里听说过,但是无论如何我都不确定100%)。 I would be really pleased if you could help me through this! 如果您能帮助我解决这个问题,我将非常高兴!

Cheers, absolado 干杯,绝对

Here below is the sample code of simple search. 以下是简单搜索的示例代码。

index.html.erb index.html.erb

<% form_tag projects_path, :method => 'get' do %>
  <p>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", :name => nil %>
  </p>
<% end %>

here your_model.rb 在这里your_model.rb

def self.search(search)
  if search
    find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
  else
    find(:all)
  end
end

here your_controller.rb 在这里your_controller.rb

def index
  @projects = Project.search(params[:search])
end

You can use a gem for this in case you do not want to implement it yourself. 如果您不想自己实现它,则可以为此使用gem。 Ransack is a very popular gem which can support all this functionality along with helpers for your views, advanced search matchers and many more. Ransack是一个非常受欢迎的宝石,它可以支持所有这些功能以及视图的帮助器,高级搜索匹配器等等。 I suggest you have a look on this before proceeding with your own implementation. 我建议您在继续自己的实现之前先看一下。

This looks like a job for Datatables . 这看起来像Datatables的工作。 I have used this many times in my apps. 我已经在我的应用程序中使用了很多次。 Aside from searching an attribute in your records, it also has sortable column feature. 除了在记录中搜索属性外,它还具有可排序的列功能。

It is important to outline that Datatable is a javascript package which builds a robust table with filter, order, pagination goodies but by default it is not done server-side. 概述Datatable是一个javascript程序包非常重要,它可以构建具有过滤器,顺序,分页功能的健壮表,但是默认情况下,它不是在服务器端完成的。

Which means that if you have a large dataset then you might be in trouble as all your records will be pumped to the client's browser as a huge json and Datatable will parse and fill in the table for you. 这意味着,如果您有一个大数据集,则可能会遇到麻烦,因为所有记录都将作为一个巨大的json被泵送到客户端的浏览器,而Datatable将为您解析并填写表。

Datatable provides support to server-side pagination, filter and so on, but then you will have to implement those features into your back-end. Datatable为服务器端分页,过滤器等提供支持,但是随后您将不得不在后端实现这些功能。

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

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