Skip to content

Instantly share code, notes, and snippets.

@diegofmp
Last active May 21, 2017 20:24
Show Gist options
  • Save diegofmp/c8f3537afa2d60bef92cd6f5123ffba9 to your computer and use it in GitHub Desktop.
Save diegofmp/c8f3537afa2d60bef92cd6f5123ffba9 to your computer and use it in GitHub Desktop.
"Advanced search" on rails & will_paginate gem
class Search < ActiveRecord::Base
def search_posts
posts= Post.all
posts= posts.contains_key(keyboards) if keyboards.present?
posts= posts.wSize(size_id) if size_id.present?
return posts
end
end
class SearchesController < ApplicationController
#...
def create
@search = Search.create(search_params)
redirect_to @search
end
def show
@active=:show
@search = Search.find(params[:id])
@searchResults= @search.search_posts.paginate(page: params[:page], per_page: 8)
end
def search_params
params.require(:search).permit(:keyboards, :size_id)
end
end
<% if @searchResults.empty? %>
<p>No results</p>
<% else %>
<% @searchResults.in_groups_of(4,false).each do |group| %> #to show in 4 columns
<div class="row">
<% group.each do |p| %>
<div class="col-sm-3">
<div class="thumbnail">
<div class="caption">
<h3>p.title</h3>
<!-- and every other thing you want to show of the post -->
</div>
</div>
</div>
<% end %>
</div>
<div class="col-sm-12" style="text-align: centered;">
</div>
<% end %>
<%= will_paginate @searchResults, renderer: BootstrapPagination::Rails, previous_label: "Anterior", next_label: "Siguiente" %>
<% end %>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment