ruby-on-railsajaxsortingfilterrific

Rails with Filterrific is not working


I don't get filterrific to work although I feel I am really close to a solution. In my App you can create search requests for products which other people can answer. Now I want to implement filterrific to sort (and later also filter) these search requests. I followed the brilliant documentation on http://filterrific.clearcove.ca/ but I do not find the reason why it is not working in my app. I don't get any errors but whatever I do it is not sorting.

This is my Search model:

    filterrific(
      default_filter_params: { sorted_by: 'created_at_desc' },
      available_filters: [
        :sorted_by,
      ]
    )

scope :sorted_by, lambda { |sort_option|
  # extract the sort direction from the param value.
  direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
  case sort_option.to_s
  when /^created_at_/
    # Simple sort on the created_at column.
    # Make sure to include the table name to avoid ambiguous column names.
    # Joining on other tables is quite common in Filterrific, and almost
    # every ActiveRecord table has a 'created_at' column.
    order("searches.created_at #{ direction }")
  else
    raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
  end
}


def self.options_for_sorted_by
    [
      ['Created at (Newest first)', 'created_at_desc'],
      ['Created at (Oldest first)', 'created_at_asc']
    ]
end  

This is my Search controller:

def index

 @filterrific = initialize_filterrific(
      Search,
      params[:filterrific],
      select_options: {
        sorted_by: Search.options_for_sorted_by
      },
      persistence_id: 'shared_key',
      default_filter_params: {},
      available_filters: [],
  ) or return
  @searches = @filterrific.find.paginate(page: params[:page], per_page: 5)

  respond_to do |format|
    format.html
    format.js
end

end

This is my index.html.erb:

<%= form_for_filterrific @filterrific do |f| %>

<div>
    Sorted by
    <%= f.select(:sorted_by, @filterrific.select_options[:sorted_by]) %>
</div>
<div>
    <%= link_to(
      'Reset filters',
      reset_filterrific_url,
    ) %>
  </div>
  <%= render_filterrific_spinner %>
  <% end %>

  <%= render 'searches/search', locals: { searches: @searches } %>

This is my partial _search.html.erb:

<div id="filterrific_results">
<% @searches.each do |search| %>
<%= time_ago_in_words(search.created_at) %>
...
<% end %>
</div>  
<%= will_paginate @searches %>

And finally, my index.js.erb:

<% js = escape_javascript(
  render(partial: 'searches/search', locals: { searches: @searches })
) %>
$("#filterrific_results").html("<%= js %>");

Why I feel close to a solution? A change of the default_filter_params from 'created_at_desc' to 'created_at_asc' is working. And also the option select list is then automatically changing to 'created at (Oldest first)'. But when I change the option select nothing is happening except the spinner shows up for a second.

Thank you for any help!


Solution

  • I just found the solution myself. I added :sorted_by to the Search controller.