Skip to content

Instantly share code, notes, and snippets.

@radavis
Created October 16, 2014 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radavis/b2825396f48d80477129 to your computer and use it in GitHub Desktop.
Save radavis/b2825396f48d80477129 to your computer and use it in GitHub Desktop.
Multiple model search
# routes.rb
get 'search' => 'search#index'
# search_controller.rb
class SearchController < ApplicationController
def index
@kitchens = Kitchen.where("city ilike ?", "%#{params[:q]}%")
@meals = Meal.where("name ilike ?", "%#{params[:q]}%")
end
end
# search input
<%= form_tag search_path, method: :get do %>
<div class="form-input">
<%= text_field_tag :search, params[:q], placeholder: "enter city or meal" %>
</div>
<div class="form-submit">
<%= submit_tag "Search" %>
</div>
<% end %>
# search/index.html.erb
<% if @kitchens %>
<h1>Kitchens</h1>
<%= @kitchens.each do |kitchen| %>
<%= link_to kitchen %>
<% end %>
<% end %>
<% if @meals %>
<h1>Meals</h1>
<%= @meals.each do |meal| %>
<%= link_to meal %>
<% end %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment