That dude
That dude

Reputation: 389

advance table search in rails

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.

Upvotes: 1

Views: 241

Answers (1)

Duyet Nguyen
Duyet Nguyen

Reputation: 543

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)

Upvotes: 1

Related Questions