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

spondbob
  • 1,523
  • 2
  • 17
  • 34

2 Answers2

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.

Vasilisa
  • 4,604
  • 3
  • 20
  • 25
  • This adds `"/dashboard"` to the existing base URL. If you're looking to change the base URL itself, this may not work. – Qasim May 26 '23 at 10:55
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}

Max Ivak
  • 1,529
  • 17
  • 39
  • 1
    Tested in kaminari '1.2.2', the `url` argument is ignored. The solution with the `params` argument works. – jibai31 Jun 24 '22 at 09:25