0

I have a pagination on a page using Kaminari and Ajax, but actually, when I click 'next' button or a number of the pagination, I haven't result and the url is doesn't updated. If I click on '2' I want my url being :

'localhost:3000/auto-ecoles/paris/paris/gp-conduite?filter=verified&page=2'

and right now if I click on '2', the content of my div disappear and my url is :

'localhost:3000/auto-ecoles/paris/paris/gp-conduite?filter=verified'

If write by my self the good url it work so, something went wrong !

here is the code, controller :

if !params[:filter] || params[:filter] == "verified"
  @models = @school.ratings.verified.page(params[:page]).per(15)
elsif params[:filter] == "unverified"
  @models = @school.ratings.unverified.page(params[:page]).per(15)
elsif params[:filter] == "allratings"
  @models = @school.ratings.page(params[:page]).per(15)
end
respond_to :html, :js

View :

  <div class="vv-tab-menu">
    <%= link_to 'Avis vérifiés', specific_school_path(filter: "verified") %>
    <%= link_to 'Avis non-vérifiés', specific_school_path(filter: "unverified") %>
    <%= link_to 'Tous les avis', specific_school_path(filter: "allratings") %>
 </div>
 <div class="vv-tab-content">
    <%= render partial: 'schools/rating', collection: @models %>
    <%= paginate @models, params: params, :remote => true %>
 </div>

and js.erb file

<% if !params[:filter] || params[:filter] == "verified" %>
  $('.vv-tab-content').html('<%= escape_javascript render(partial: "schools/rating", collection: @models) %>');
  $('.vv-tab-content').html('<%= escape_javascript(paginate(@models, params: params, :remote => true).to_s) %>');
<% elsif params[:filter] == "unverified" %>
  $('.vv-tab-content').html('<%= escape_javascript render(partial: "schools/rating", collection: @models) %>');
  $('.vv-tab-content').html('<%= escape_javascript(paginate(@models, params: params, :remote => true).to_s) %>');
<% elsif params[:filter] == "allratings" %>
  $('.vv-tab-content').html('<%= escape_javascript render(partial: "schools/rating", collection: @models) %>');
  $('.vv-tab-content').html('<%= escape_javascript(paginate(@models, params: params, :remote => true).to_s) %>');
<% end %>

Do you know how to pass the page number in my url, using Ajax ? Does something is wrong in my code ?

Antonin Mrchd
  • 656
  • 3
  • 9
  • 31
  • I think you don't need the `params: params` part, remove it and see if it works: `paginate @models, remote: true` – fanta Mar 03 '17 at 18:24
  • @fanta thank you for your answer ! I remove params: params, but it still not work, I haven't '&page=2' in my URL .. – Antonin Mrchd Mar 04 '17 at 13:31
  • AJAX request does not change the url.If you want to show page number then you should leave AJAX request and send HTTP request. But still if you want to change the url then you can try like `window.location.href + "&page= params[page_number]"` which is a bad practice. – monsur Mar 04 '17 at 18:41
  • @monsur thank you for the advice ! I want to use Ajax for not refreshing the page, so how can I do that and see the next page ? right now, If I click on '2', the ratings disappear .. – Antonin Mrchd Mar 04 '17 at 18:47
  • 1
    then you should not use `remote: true` instead you have to write manual AJAX request.like `$.ajax({ method: "get", url: "your_pagination_path", page: "your page number" //here 2 as for example. });` And then on success event you can change the url like i said before. Hope that helps. – monsur Mar 04 '17 at 19:10
  • @monsur ok I'm going to try ! but, where do I put the ajax code ? in the js.erb file ? because I haven't click event – Antonin Mrchd Mar 04 '17 at 19:22
  • 1
    @AntoninMrchd you need the click event of the pagination number button the ajax call should be your html.erb file. – monsur Mar 05 '17 at 10:02

0 Answers0