Last active
January 2, 2016 04:39
-
-
Save abangratz/8252260 to your computer and use it in GitHub Desktop.
Sample decision on parameter (rails 4)
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
<div id="search_block"> | |
<%= form_tag(action: 'search', method: :put) do %> | |
<%= text_field_tag(:search, params[:search], placeholder: 'Search ...') %> | |
<%= submit_tag('Go') %> | |
<% end %> | |
</div> | |
<% if @rentals.blank? %> | |
<%# Notify user of missing parameter or empty result set %> | |
<h4>Please give a valid search term</h4> | |
<div class="help-block"> | |
<% if @rentals %> | |
<%# No results %> | |
No rentals found | |
<% else %> | |
<%# No search term %> | |
Please input a valid search term. | |
<% end %> | |
</div> | |
<% else %> | |
<% rentals.each do |rent| %> | |
<%# ... output rent details %> | |
<% 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
class SearchController < ApplicationController | |
def index | |
unless params[:search].blank? # avoid empty or null search parameter | |
@rentals = Rentals.where('name LIKE ?', "%#{params[:search]}%") | |
end | |
@parishes = Parish.all # using a separate function for a single line is | |
@counties = Counties.all # just increasing the stack level, doesn't help readability | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the additional details!