简单来说
使用查询字符串参数过滤对象列表时,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
问题
当表单用于过滤 LearningObjects 时,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 会生成这些格式错误的嵌套链接?