In form sorting using ransack gem not working

白昼怎懂夜的黑 提交于 2019-12-10 12:27:01

问题


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:

Does anybody know what could possibly be wrong?

Thanks for all the help!


回答1:


In your controller you missed the line

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

which should be after

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


来源:https://stackoverflow.com/questions/13241481/in-form-sorting-using-ransack-gem-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!