kaminari pagination on custom url
Asked Answered
R

2

6

I have a case on url that generated by kaminari paginate as the url depends on a condition. Here is my routes

concern :search do
  scope '/search', as: :search do
    get '/', to: 'users#search'
    get '/schedule/:id', to: 'schedules#doctor', as: :schedule
  end
end

concerns :search

scope :dashboard, as: :dashboard do
  concerns :search
end

As you can see, the :search is accessible through /search and /dashboard/search. The problem is, paginate @doctors gives /search which basically will goes to search_path even though I'm on dashboard_search_path (it should gives /dashboard/search path).

My question is, how can I pass a custom path to paginate? I'd like paginate to use search_path when I open /search and use dashboard_search_path when I'm on /dashboard/search/path.

You don't have to provide how to decide /search or /dashboard/search, I just need to know how to pass it to paginate as an argument. Ta

Rothwell answered 27/1, 2015 at 7:55 Comment(0)
F
2

Kaminari accepts parameters for paginate, one of them is params for the links. It works the same as url_for in rails. Didn't test it, but try for dashboard pages, should work.

paginate @doctors, params: { script_name: "/dashboard" }

From docs for url_for:

:script_name - specifies application path relative to domain root. If provided, prepends application path.

Fleshpots answered 19/2, 2019 at 13:0 Comment(1)
This adds "/dashboard" to the existing base URL. If you're looking to change the base URL itself, this may not work.Handcraft
H
0

use url param for paginate and have your custom code in proc which will define the final url:

paginate @doctors, url: proc{|page| some_condition ? search_path : dashboard_search_path}

Holohedral answered 19/2, 2021 at 1:10 Comment(1)
Tested in kaminari '1.2.2', the url argument is ignored. The solution with the params argument works.Along

© 2022 - 2024 — McMap. All rights reserved.