Skip to content

Instantly share code, notes, and snippets.

@rossnelson
Created February 8, 2016 15:45
Show Gist options
  • Save rossnelson/5cffaf83f736d9d9981e to your computer and use it in GitHub Desktop.
Save rossnelson/5cffaf83f736d9d9981e to your computer and use it in GitHub Desktop.
Rails 3-4 simple search
= form_tag path_to_results_path, method: :post do
%p
= text_field_tag :search, params[:search]
= submit_tag "Search", name: nil
def self.search(term)
if term
term = "%#{term}%"
where('body LIKE ? OR name LIKE ? OR keyword LIKE ?', term, term, term)
else
all
end
end
def index
@results = MyModel.search(params[:search])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment