Skip to content

Instantly share code, notes, and snippets.

@Colt
Created February 28, 2014 01:41
Show Gist options
  • Save Colt/9263565 to your computer and use it in GitHub Desktop.
Save Colt/9263565 to your computer and use it in GitHub Desktop.

Exercise - Instructor

Shirt Search Code Along

###Part 1: Review Time 20 min

###Part 2: Search! Time: 90min

  • Create a search box in index.html.erb
<%= form_tag "/", method: "GET" do %>
  <%= text_field_tag :q, params[:q] %>
  <%= submit_tag "Search" %>
  <% end %>
  • Add to index method - extend index action to handle a search param
def index
    @shirts = Shirt.search_for(params[:q])
end

  • See what happens when we run this! ERROR!
  • Create a method on the Model to get our records passed in from search box
def self.search_for(query)
    where('name LIKE :query OR description LIKE :query', query: "%#{query}%")
  end
  
  • Display search results in the search.html.erb
<% @shirts.each do |shirt| %>
  <section>
    <header><%= shirt.name %></header>
    <p><%= shirt.description %></p>
    <%= image_tag shirt.image %>
  </section>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment