Search code examples
ruby-on-railsrubyransack

In form sorting using ransack gem not working


I've been trying (unsuccessfully so far) to use the out of the box in-form sorting provided by the ransack gem, and the result of it unfortunately is an empty form.

Here's what I've done so far:

Controller:
    class LandingController < ApplicationController
      layout "landing2"
      def index
        @zones = Zone.all
        @zone_id ||= 1
        @q = Property.search(params[:q])
        @q.build_sort if @q.sorts.empty?
      end
    end

View (partial):

<%= search_form_for @q, :url => home_path, :html => { :method => :get } do |f| %>
  <%= f.sort_fields do |s| %>
    <%= s.sort_select %>
  <% end %>
  <%= f.submit "Sort" %>
<% end %>

And the result is:

Ransack sort form(empty)

Does anybody know what could possibly be wrong?

Thanks for all the help!


Solution

  • In your controller you missed the line

    @properties = @q.result(:distinct => true)
    

    which should be after

    @q = Property.search(params[:q])