Created
October 16, 2014 18:51
-
-
Save radavis/b2825396f48d80477129 to your computer and use it in GitHub Desktop.
Multiple model search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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