使用kaminari +查询字符串排序的Paginate会导致奇怪的链接

时间:2012-11-26 21:41:18

标签: ruby-on-rails kaminari

简介

使用查询字符串参数过滤对象列表时,Kaminari分页助手中的链接是错误的。

背景

我有一个动作LearningObjects#index支持我通过Kaminari实现自我和分页的基本排序。

使用执行GET请求的普通表单完成排序。

控制器:

class LearningObjectsController < AdministrationController
  respond_to :json, :html
  load_and_authorize_resource

  # GET /learning_objects
  # GET /learning_objects.json
  def index
    # Searching
    if LearningObject.has_search_params(params)
      @learning_objects = LearningObject.search(params)
    end

    # Pagination
    @learning_objects = @learning_objects.page(params[:page]).per(params[:per])
    respond_with @learning_objects, serializer: LearningObjectPageSerializer, total_records: @learning_objects.total_count
  end

  # other actions...#
end

视图

%h1= t("actions.learning_object.index")

.well
  %h4= t("actions.filter")
  = form_tag(learning_objects_path, method: :get, class: "form-search") do
    = select_tag :subject_id, 
      options_from_collection_for_select(@subjects, "id", "name", params[:subject_id].to_i), 
      prompt: t("activerecord.models.subject")
    = select_tag :stage_id,
      options_from_collection_for_select(@stages, "id", "name", params[:stage_id].to_i), 
      prompt: t("activerecord.models.stage")
    = text_field_tag :free_text, params[:free_text], placeholder: t("actions.write_something")
    = submit_tag t("actions.filter"), class: :btn
  = link_to t("actions.clear"), learning_objects_path, class: :btn

%p= t("actions.learning_object.show_number", count: @learning_objects.total_count, total: @total)
= paginate @learning_objects
= table_for @learning_objects do |t|
  = t.data actions: :all do
    =t.cell(:name) {|l| link_to l, l}
    =t.cell(:downloads) {|l| "#{l.downloads} #{t("misc.times", count: l.downloads)}"}
    =t.cell(:language)
    =t.cell(:active) {|l| tag("i", class: l.active ? "icon-ok" : "") }


= link_to t("actions.learning_object.new"), new_learning_object_path

问题

当表单用于过滤学习对象时,Kaminari生成的分页链接都很奇怪。

例如,如果地址如下所示:http://localhost:3000/learning_objects?utf8=%E2%9C%93&subject_id=&stage_id=&free_text=obj&commit=Filtrera

然后分页链接如下所示:http://localhost:3000/subjects//learning_objects?commit=Filtrera&free_text=obj&page=2&stage_id=&utf8=%E2%9C%93

我希望分页链接看起来像这样:http://localhost:3000/learning_objects?utf8=%E2%9C%93&subject_id=&stage_id=&free_text=obj&commit=Filtrera&page=2

问题

为什么当查询字符串中存在搜索字词时,Kaminari会生成这些格式错误的嵌套链接?

0 个答案:

没有答案