ruby-on-railsransackkaminari

how url_for works - problems with ransack and kaminari


I'm using Rails 4 with Ransack and Kaminari, I have this route:

resources :domains do
  resources :backlinks do
    collection { post :search, to: 'backlinks#index' }
  end
end

I discovered that kaminari pagination generates wrong links, like that:

/domains/16/backlinks/search?page=2

instead of

/domains/16/backlinks?page=2

I explored the source code of the Kaminari gem and saw that it simply uses url_for helper with params:

class Tag
  def initialize(template, options = {}) #:nodoc:
    @template, @options = template, options.dup
    @params = @options[:params] ? template.params.merge(@options.delete :params) : template.params
  end
  ...
  def page_url_for(page)
    @template.url_for @params.merge(@param_name => (page <= 1 ? nil : page))
  end
end

Then I tested this simple code in my view:

= url_for params

(params are {"action"=>"index", "controller"=>"backlinks", "domain_id"=>"16"})

and yes, it generated

/domains/16/backlinks/search

My question is why does this extra collection route (search) that is not even "get" influence url creation that much... is this "normal"?

My second question is how to get out of the problem and somehow tell Kaminari to generate correct links even if I keep that extra route.


Solution

  • Your route should look as follows to avoid search:

    resources :domains do
      resources :backlinks
    end