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