0

I am using Kaminari gem to paginate using ajax

i have three paginations on the same page

    @all_questions = Question.where.not(id: current_user.question_ids).page(params[:unanswered]).per(1)

    @wrong = Tracker.where("user_id = ? AND correct = ?", current_user, false).page(params[:wrong_answers]).per(1)

    @answered = Tracker.where("user_id = ? AND answered = ?", current_user, true).page(params[:all_answered]).per(1)

while the last two of the above instance variables correctly work. The first one when i click the next button, while i see the ajax request happening in the rails console, it does not refresh the page.

in my view

<%= paginate @all_questions, :remote => true, :param_name => "unanswered" %>
<%= paginate @wrong, :remote => true, :param_name => "wrong_answers" %>
<%= paginate @answered, :remote => true, :param_name => "all_answered" %>

Anyone knows why?

html

     <section role="tabpanel" aria-hidden="true" class="content" id="panel2-2">
        <div class="row">
          <div class="large-10 large-offset-1 columns">
            <div class="panel questions-progress-panel">
              <ul>
                <div id="unanswered">
                  <%= render partial: "all_questions" %>
                </div>
                <div id="paginator4" class="text-center paginator">
                  <%= paginate @all_questions, :remote => true, :param_name => "unanswered" %>
                </div>
              </ul>
            </div>
          </div>
        </div>
      </section>

@all_questions partial

<% @all_questions.each do |q| %>
  <li>
    <div class="wrapper">
      <div class="row">
        <div class="large-8 medium-8 small-8 columns">
          <p class="question-title"> <%= q.description %> </p>
        </div>
        <div class="large-4 medium-4 small-4 columns text-right">
          <%= link_to "Go", category_question_path(q.category,q), class:"button round success" %>
        </div>
      </div>
    </div>
  </li>
<% end %>
Petros Kyriakou
  • 5,214
  • 4
  • 43
  • 82
  • What is `@all_questions` like? Does it contain questions at least? – born4new Apr 08 '16 at 09:46
  • yes it contains two after executing the query. I can see them showing, shows two pages as well [1,2 next last] in pagination controls, its just not the pagination working. Does Kaminari expect a certain structure or something? – Petros Kyriakou Apr 08 '16 at 09:46
  • If I remember correctly Kaminari expects a `Relations` type of the structure. Could you inspect in console what type of the class is using all of those results above e.g. put `p @all_questions` , `p @wrong` etc. in your code and run it, and then take a look at a console, what it displays. What type of those relations are – Aleks Apr 08 '16 at 09:56
  • Hm, but if it displays the results correctly at least on one of the pages, then it should be working. Check the web-browser's console for javascript errors instead of Rails errors, maybe that will give you a hint. Also, is the `unanswered` parameter being set when paginating those results for all questions? – Aleks Apr 08 '16 at 09:57
  • @Aleks i have used `p` on the first two instance variables both display `# – Petros Kyriakou Apr 08 '16 at 10:04
  • @PetrosKyriakou how does you corresponding `js.erb` looks like ? – dp7 Apr 08 '16 at 10:13
  • @dkp thank you! i totally forgot that i have to add some lines in my .js.erb to change the view! that was it! cheers! – Petros Kyriakou Apr 08 '16 at 10:16
  • @dkp sorry for taking you the answer on previous question. Maybe you can create a new answer on this to get accepted on this one, if Petros accepts it, or course, as you have pointed to the solution – Aleks Apr 08 '16 at 10:20
  • 1
    @Aleks You answer was indeed simply efficient one!! :) Hope Petros get through this problem! – dp7 Apr 08 '16 at 10:32
  • 1
    cheers to both of you guys! – Petros Kyriakou Apr 08 '16 at 10:42

1 Answers1

1

Your corresponding js.erb could look like this:

$('#unanswered').html('<%= escape_javascript render(all_questions) %>');
$("#paginator4").html('<%= escape_javascript(paginate(@all_questions, :remote => true).to_s) %>');
dp7
  • 6,651
  • 1
  • 18
  • 37