Last active
September 1, 2016 02:29
-
-
Save hernandezalek/9d86f7d80ab4abccadfd4db6ccc42c91 to your computer and use it in GitHub Desktop.
Simple 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
class Article < ActiveRecord::Base | |
belongs_to :user | |
def self.search(search) | |
where("title LIKE ?", "%#{search}%") | |
end | |
end |
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
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 |
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
<%= 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