在Rails中进行提前表格搜索 [英] advance table search in rails

查看:78
本文介绍了在Rails中进行提前表格搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我生成了一个名为data的支架,并在表格中填写了姓名,学校,学院,意见,兴趣. 现在,我要添加一个搜索,如果有人在搜索栏中输入NAME,则他们应该只将所有名称作为结果;如果有人输入了大学,则应该仅获取所有大学.

In my app i have generated a scaffold called data and had filed the table with name, school, college, views, interest. Now i want to add a search that if someone put NAME in the search bar, they should get all the names only as results, if someone had put college, they should get all the college only.

对于Rails来说还很新,所以我尝试观看一些教程,并添加了它,但这不是我想要的

Am pretty new to rails, so i tried watching some tutorials, and had added this but this is not what i want

<%= form_tag data_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search" %>
<% end %>

在控制器中

  def index
    @data = Datum.where (["name LIKE ?", "%#{params[:search]}%"])
  end

这添加了一个搜索,但没有添加我上面提到的搜索类型.

This had added a search but not the type of search i had mentioned above.

推荐答案

我认为是在控制器上

def index
  if params[:search].present?
    search_type = params[:search]
    @data=Datum.all.map(&search_type.to_sym)
  end
end

注意:我建议将您的text_field_tag :search更改为dropdown (select_tag)

Note: I recommend change your text_field_tag :search to dropdown (select_tag)

这篇关于在Rails中进行提前表格搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆