Skip to content

Instantly share code, notes, and snippets.

@abangratz
Last active January 2, 2016 04:39
Show Gist options
  • Save abangratz/8252260 to your computer and use it in GitHub Desktop.
Save abangratz/8252260 to your computer and use it in GitHub Desktop.
Sample decision on parameter (rails 4)
<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 %>
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
@alobban
Copy link

alobban commented Jan 6, 2014

Thanks for the additional details!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment