advance table search in rails

倖福魔咒の 提交于 2019-12-24 08:13:43

问题


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.

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 %>

in controller

  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.


回答1:


I think, on controller

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

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



来源:https://stackoverflow.com/questions/40955019/advance-table-search-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!