Skip to content

Instantly share code, notes, and snippets.

@hernandezalek
Last active September 1, 2016 02:29
Show Gist options
  • Save hernandezalek/9d86f7d80ab4abccadfd4db6ccc42c91 to your computer and use it in GitHub Desktop.
Save hernandezalek/9d86f7d80ab4abccadfd4db6ccc42c91 to your computer and use it in GitHub Desktop.
Simple search
class Article < ActiveRecord::Base
belongs_to :user
def self.search(search)
where("title LIKE ?", "%#{search}%")
end
end
def index
@articles = Articles.all
if params[:search]
@articles = Article.search(params[:search]).order("created_at DESC")
else
@articles = Article.all.order("created_at DESC")
end
end
<%= form_tag(articles_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Buscar articulos", class: 'search' %>
<%= submit_tag "Buscar", :title => nil, class: 'btn' %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment