简体   繁体   中英

rails kaminari pagination inappropriate link when using ransack search

here is my route.rb:

match "search" => "welcome#search" , via: [:get, :post]

and here is my search method in welcome controller:

def search
    @search = Poem.search(params[:q])
    @poems = @search.result(distinct: true).page(params[:page]).per(15)
  end

and here is my search view:

<%= search_form_for @search, url: search_path , html: { method: :post }  do |f| %>
  <%= f.label :content_cont %>
  <%= f.text_field :content_cont %>
  <%= f.submit %>
<% end %>

<%= render :partial => 'show' %>

and here is my show partial :

<%= paginate @poems%>
.... ( other stuff)

I'm using ransack to perform advanced search it does work but pagination links are like this:

http://0.0.0.0:8080/search?authenticity_token=oiOmBeY80DH0zAN5lST%2BCo355phcr3TGqOSlTQ1qtlc%3D&commit=Search&page=2&q%5Bcontent_cont%5D=&utf8=%E2%9C%93

kind of post/get problem.

any suggestion?

I know this is like 2 years late, but this other solution works for me and I'm just cross posting it here:

https://github.com/amatsuda/kaminari/issues/182#issuecomment-122124373

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM