Ransack: Will_Paginate

Created on 5 Mar 2012  路  15Comments  路  Source: activerecord-hackery/ransack

Out the box I'm struggling to get will_paginate to work with ransack. It filters the first page of results just fine but doesn't remember the query for the subsequent pages.

Am I just expecting too much?

Most helpful comment

To the contrary, this does not work with will_paginate and SEARCH POST, at least not by default. If you use POST in your search method, parameters will not be passed to will_paginate.

Problem being will_paginate does not generate a form that will post, instead it generates links that will need to have params[:q] passed, which will end up being ugly as end users start moving about pagination.

This will actually get will_paginate working with POST, but the next page will then be switched to "GET" :
<%= will_paginate(@search, :params => { q: params[:q]}) %>

The way around all this is to look into will_paginate Link Rendering, and have a form with post be used to scroll through pages: https://github.com/mislav/will_paginate/wiki/Link-renderer.

Hope this can be helpful to someone.

All 15 comments

Ernie says it's possible: #78

I ended up switching to kaminari which just worked

On 8 Mar 2012, at 21:46, Will [email protected] wrote:

Ernie says it's possible: #78


Reply to this email directly or view it on GitHub:
https://github.com/ernie/ransack/issues/80#issuecomment-4403403

Would you elaborate?

Well I tried it with will paginate and it didn't work out the box. Second page lost the search term. I implemented the kaminari paging gem which is very similar and it 'just worked' ;)

Hi @morgz! Do you have an application that I could use to reproduce this issue? Thanks!

Fwiw, I'm using will_paginate (3.0.3), rails (3.2.2), and ransack (0.6.0) with the ReadMe's "Simple Mode" (ie: query string params, not post).

In the example below, @people is an ActiveRecord::Relation instance so I expected pagination to work fine and it does, no problems:

@people = @q.result(:distinct => true).paginate(:page => params[:page], :per_page => 40) if params[:q]

This gem is a fantastic time-saver. Thank you Ernie.

Closing until there can be proven there's an issue here.

I'm having the same issue. When I switch to page 2 for ex :

Request

Parameters:

{"page"=>"2",
"id"=>"search"}

The request params are lost

will_paginate has worked fine together with Ransack since the beginning (I use the two together in production since 3 years), so the issue is probably with your code.

If you find a new issue, would you please file a proper issue as per the Contributing Guide? Thanks.

Well, Ransack (via get) and will_paginate works fine for me, but if I use ransack (via post) the pagination is lost

Thanks @ffscalco. Could you please share a failing test and your Ransack/Rails/Ruby versions?

Sure @jonatack!
Versions:

  • Ruby: 2.1.2
  • Rails: 4.1.7
  • Will_paginate: 3.0.6
  • Ransack: 1.5.1

But I guess this is some "miss code"
The pagination doesn't work because the links for pages in will_paginate is via GET, of course it wouldn't work if ransack is in POST mode ;]

I don't know the best way to treat this. If is modify the links in will_paginate to post page instead get, or just use some javascripts.

@ffscalco If you go here this person (rvsingh) has resolved the issue:

http://railscasts.com/episodes/370-ransack?view=comments

Per his comments, make this change in routes. And it works.

resources :styles do
  collection do
    match 'search' => 'styles#index', :via => [:get, :post], :as => :search
  end
end

FWIW the README also suggests something similar for the routes.

To the contrary, this does not work with will_paginate and SEARCH POST, at least not by default. If you use POST in your search method, parameters will not be passed to will_paginate.

Problem being will_paginate does not generate a form that will post, instead it generates links that will need to have params[:q] passed, which will end up being ugly as end users start moving about pagination.

This will actually get will_paginate working with POST, but the next page will then be switched to "GET" :
<%= will_paginate(@search, :params => { q: params[:q]}) %>

The way around all this is to look into will_paginate Link Rendering, and have a form with post be used to scroll through pages: https://github.com/mislav/will_paginate/wiki/Link-renderer.

Hope this can be helpful to someone.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

edgarbarrero picture edgarbarrero  路  4Comments

itsalongstory picture itsalongstory  路  3Comments

seanfcarroll picture seanfcarroll  路  4Comments

tagrudev picture tagrudev  路  3Comments

mbajur picture mbajur  路  5Comments