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
@abangratz
Copy link
Author

Reference: https://plus.google.com/107996316032413459788/posts/SW114GqqARy

Reasoning:

  1. Do data related stuff (filtering etc.) in the controller.
  2. Use the view to check if there are results and react accordingly. Alternatively: use different views/partials depending on parameters or result.
  3. Don't use methods to replace a single line of code. It just confuses and adds a stack level for ruby to parse.

@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