ruby on rails - How to search by column attributes in Filterrific -
i'm trying create search , filter filterrific, part code working expected , i'm able implement basic sorting , search following docs. i've added custom :with_organizer
filter filters name column. dropdown displays names properly, nothing shows in results list. can search name column , list updated.
how make work expected?
my controller
def index # @events = event.where(published:true).order('start_date asc').order('start_time asc') @filterrific = initialize_filterrific( event, params[:filterrific], select_options: { sorted_by: event.options_for_sorted_by, with_organizer: event.options_for_organizer }, persistence_id: 'shared_key', ) or return @events = @filterrific.find.page(params[:page]) respond_to |format| format.html format.js end rescue activerecord::recordnotfound => e # there issue persisted param_set. reset it. puts "had reset filterrific params: #{ e.message }" redirect_to(reset_filterrific_url(format: :html)) , return end
my model
filterrific( default_filter_params: { sorted_by: 'start_date_asc' }, available_filters: [ :sorted_by, :search_query, :with_organizer ] ) scope :with_organizer, lambda { |names| where(name: [*names]) } def self.options_for_organizer order('lower(name)').map { |e| [e.name, e.id] } end scope :sorted_by, lambda { |sort_option| direction = (sort_option =~ /desc$/) ? 'desc' : 'asc' case sort_option.to_s when /^title/ order("lower(events.title) #{ direction }") when /^city/ order("lower(events.city) #{ direction }") when /^start/ order("lower(events.start_date) #{ direction }") else raise(argumenterror, "invalid sort option: #{ sort_option.inspect }") end } def self.options_for_sorted_by [ ['title(a-z)', 'title_asc'], ['date (newest first)', 'start_desc'], ['date (oldest first)', 'start_asc'], ['city (a-z)', 'city_asc'] ] end scope :search_query, lambda { |query| # searches students table on 'first_name' , 'last_name' columns. # matches using like, automatically appends '%' each term. # case insensitive mysql, case # sensitive postgresql. make work in both worlds, # downcase everything. return nil if query.blank? # condition query, parse individual keywords terms = query.downcase.split(/\s+/) # replace "*" "%" wildcard searches, # append '%', remove duplicate '%'s terms = terms.map { |e| ('%'+ e.gsub('*', '%') + '%').gsub(/%+/, '%') } # configure number of or conditions provision # of interpolation arguments. adjust if # change number of or conditions. num_or_conds = 4 where( terms.map { |term| "( lower(events.title) ? or lower(events.city) ? or lower(events.state) ? or lower(events.name) ? )" }.join(' , '), *terms.map { |e| [e] * num_or_conds }.flatten ) }
my view
<%= form_for_filterrific @filterrific |f| %> <div class="row"> <div class="3u 12u$(mobile)"> search <%# give search field 'filterrific-periodically-observed' class live updates %> <%= f.text_field( :search_query, class: 'filterrific-periodically-observed' ) %> </div> <div class="3u 12u$(mobile)"> sorted <%= f.select(:sorted_by, @filterrific.select_options[:sorted_by]) %> </div> <div class="3u 12u$(mobile)"> organizer <%= f.select( :with_organizer, @filterrific.select_options[:with_organizer], { include_blank: '- -' } ) %> </div> <div class="3u 12u$(mobile)"> <%= link_to( 'reset filters', reset_filterrific_url, ) %> </div> <%# add automated spinner form when list refreshed %> <%= render_filterrific_spinner %> </div> <% end %> <%= render( partial: 'events/list', locals: { events: @events } ) %>
list partial
<div id="filterrific_results"> <table> <tr> <th><%= filterrific_sorting_link(@filterrific, :title) %></th> <th><%= filterrific_sorting_link(@filterrific, :city) %></th> <th><%= filterrific_sorting_link(@filterrific, :start_date) %></th> </tr> <% events.each |event| %> <tr> <td><%= link_to(event.title, event_path(event)) %></td> <td><%= event.city %></td> <td><%= event.start_date %></td> </tr> <% end %> </table> </div>
Comments
Post a Comment