Skip to content

Pagination generates ugly url's (includes all params) #182

Closed
Godisemo opened this Issue Dec 1, 2011 · 13 comments
@Godisemo
Godisemo commented Dec 1, 2011

I have an issue with ugly url's. A post action generating a view with a pagination. The links end up really nasty

/company/stores?_method=put&authenticity_token=sYdz9iRD59ke%2BAYB6kfgqTLHZ2VlL9GMh8m5kDrGxE4%3D&city_search=&commit=Update+Store&page=3&store%5Baddress%5D=Vipem%C3%B6llev%C3%A4gen+113&store%5Bcity%5D=Lund&store%5Bpostcode%5D=224+60&utf8=%E2%9C%93

My temporary solution is to manually reset all params like

paginate @list_stores, remote: true, :params => { :controller => 'stores', :action => 'index', _: nil, _method: nil, authenticity_token: nil, store: nil, commit: nil, utf8: nil}

I would really like a way to only include some defined parameters instead of my hack to exclude these.

@ehoch
ehoch commented Jan 10, 2012

Felt like bumping this. This isn't just ugly for me, but actually produces broken urls for my nested resources. It's important to note this is only after a non-Kaminari AJAX call that pulls in new pagination.

@koraktor

I have the same problem.
I guess this will probably hit anyone who wants to paginate anything linking to different controllers, actions or params.

Resetting seems to work, but isn't DRY.

@ehoch
ehoch commented Jan 13, 2012

Right, I used Godisemo's hack from above. I'd be fine with it if I could just specify the params of the controller and action and it was a {}.except

@rin
rin commented Jul 31, 2012

this issue made me switch back to will_paginate for my current project.
still, I would appreciate a fix.

@wbyoung
wbyoung commented Nov 11, 2013

Is this for a version of Rails less than 3.2?

@wbyoung wbyoung added a commit to wbyoung/kaminari that referenced this issue Nov 11, 2013
@wbyoung wbyoung Better page URL generation.
This adds tests for #197 and may also address issue #182. It does so
by changing the URL generation to remove the page query parameter
when present. If the generated URL uses the page number in the URL
directly, it will always be present. This also leads to a possible
resolution for #44.
9f89e3b
@wbyoung wbyoung added a commit to wbyoung/kaminari that referenced this issue Nov 11, 2013
@wbyoung wbyoung Better page URL generation.
This adds tests for #197 and may also address issue #182. It does so
by changing the URL generation to remove the page query parameter
when present. If the generated URL uses the page number in the URL
directly, it will always be present. This also leads to a possible
resolution for #44.
47f469a
@yuki24 yuki24 added a commit that closed this issue Mar 13, 2014
@yuki24 yuki24 Exclude basic form params from pagination links
Even though the basic form params will be blacklisted, other params
submitted from a form still gets appended to the links. However, there
is no good way to detect what should be excluded and what shouldn't. If
a particular parameter(e.g. params[:user]) should be excluded, the
example below could be a workaround:

    paginate @users, params: {user: nil}

fixes #131. fixes #182
2fd7d36
@yuki24 yuki24 closed this in 2fd7d36 Mar 13, 2014
@michaelfeihstel

I'm sorry to revive this old issue, but it still seems to be a problem when using Kaminari and Ransack: You recommend to set unwanted params to nil, but in that case I'm not able to paginate my search results which use the Ransack default "q" parameter.

That means currently I have the following options:

  • use pretty URLs like /posts/search/page/2 - in that case Ransack will forget its search results as soon as I paginate to anything but the first page. Page 2, for example, won't show the second page of search results, but instead the second page of the whole collection, Post.all in thsi case. Probably because the q param is being overwritten.

  • use ugly URLs like /posts/page/2?q=(a bunch of search options here) - search will work in any page now, it's not pretty though

Is there any other option I'm missing? Can I preserve the q paramater without actually showing it in URLs?

@anquinn
anquinn commented Feb 13, 2015

Seconding @thirdsun question. I'm using Ransack as well and get overly ugly URLs. Is there a work around for this?

I have ransack searching with POST. Everything works fun until you go to page two, when is appends all the search params back onto the URL.

@michaelfeihstel

I never got around this issue, though I didn't really try very hard back then. A silver lining might be the fact that users are able to copy and link to specific search result pages which wouldn't be possible with example.com/collection/search/page/2 - so I left it those URLs ugly for the time being.

@anquinn
anquinn commented Feb 13, 2015

Although not exactly the same as your issue, I found a solution on stackoverflow that I adapted. Basic idea is this:

def index
    @search = Contact.search(params[:q])
    @contacts = @search.result.page(params[:page])
    @pagination_params = normalize_pagination_params
end

#fix for ugly URLs with kaminari
private
    def normalize_pagination_params
       params.inject({}) do |params_hash, p|
          unless p[0]=="controller"
            params_hash[p[0]] = nil
           end
          params_hash
         end
    end

and then in the template:
<%= paginate @contacts, params: @pagination_params %>

This allows me to search with POST, but still use GET pagination. Unfortunately it doesn't keep the search params showing after you switch pages. Keeping the params would be nicer.

@Jimeh87
Jimeh87 commented May 12, 2015

I realize this was closed a year ago, but I feel like this is still not working in an intuitive manner. Instead of forcing the developer to blacklist parameters, kaminari should take a page from strong parameters and force the developer to whitelist parameters. This would allow for much more predictable behaviour. Thoughts?

@yuki24
Collaborator
yuki24 commented May 12, 2015

@Jimeh87 Feel free to submit a pull request. Any contribution is appreciated.

@aalvarado

I've found a way to just basically do a post appending the value of the pagination link as an input field to the form, that way you can avoid having the url params in your address bar and also potentially prevent an HTTP 414 error.

$(function() {
  $('.pagination a').on('click', function(e){
    e.preventDefault();
    e.stopImmediatePropagation()
    $('form').append("<input type='text' name='page' value=" + e.target().text() + "/>");
    $('form').submit();
  });
});

Please let me know if this solves your issue or it's not what you're looking for!

Hope it helps, have fun!

@ignatiusreza

@Jimeh87 @yuki24 anyone working on a PR yet? If not, I would like to take a stab at whitelisting params

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.