Skip to content

Latest commit

 

History

History
48 lines (37 loc) · 1.42 KB

notes.md

File metadata and controls

48 lines (37 loc) · 1.42 KB

#MyApp/app/config/routes.rb get '/search' => 'pages#search', :as => 'search_page'

#Path
MyApp/app/view/pages/ #File search.html.erb

Search Result

<% @results.each do |result| %> <%= result.category.name %> <%= result.name %>
<% end %>

#for layout application.html.erb <%= form_tag(search_page_path, :method => "get",
class: 'navbar-form navbar-left') do %>

<%= search_field_tag :search, params[:search], placeholder: "Search", class: "form-control" %>
<%= button_tag "", :class => 'btn btn-info glyphicon glyphicon-search',:name => nil%>
<% end %>

#MyApp/app/controllers/pages_controller.rb def search
if params[:search].blank?
redirect_to(root_path, alert: "Empty field!") and return
else
#@parameter = params[:search].downcase
#@results = Store.all.where("lower(name) LIKE :search", search: @parameter) @results = Item.joins(:category).search(params[:search]).order("categories.name DESC")
end
end

#MyApp/app/models/item.rb

def self.search(search)
where("lower(categories.name) LIKE :search OR lower(items.name) LIKE :search", search: "%#{search.downcase}%").uniq
end

def self.search(search)
where("lower(items.description) LIKE :search OR lower(items.name) LIKE :search", search: "%#{search.downcase}%, %#{search.downcase}%").uniq
end