From 81becddbd56ffa791ed0d955aa738fe3dbb24c06 Mon Sep 17 00:00:00 2001 From: nilsding Date: Sat, 3 Jan 2015 19:24:51 +0100 Subject: [PATCH] added pagination to answers --- app/controllers/question_controller.rb | 6 +++++- app/views/question/show.html.haml | 6 ++++++ app/views/question/show.js.erb | 8 ++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 app/views/question/show.js.erb diff --git a/app/controllers/question_controller.rb b/app/controllers/question_controller.rb index 4789a8d2..54fac76c 100644 --- a/app/controllers/question_controller.rb +++ b/app/controllers/question_controller.rb @@ -1,6 +1,10 @@ class QuestionController < ApplicationController def show @question = Question.find(params[:id]) - @answers = @question.answers.reverse_order + @answers = @question.answers.reverse_order.paginate(page: params[:page]) + respond_to do |format| + format.html + format.js + end end end diff --git a/app/views/question/show.html.haml b/app/views/question/show.html.haml index e0b681e3..fe74986c 100644 --- a/app/views/question/show.html.haml +++ b/app/views/question/show.html.haml @@ -18,6 +18,12 @@ - @answers.each do |a| = render 'shared/answerbox', a: a, show_question: false + #pagination= will_paginate @answers, renderer: BootstrapPagination::Rails, page_links: false + + - if @answers.next_page + %button#load-more-btn.btn.btn-default{type: :button, data: { current_page: @answers.current_page }} + Load more + - if user_signed_in? and !current_user.answered? @question and current_user != @question.user .panel.panel-default#q-answer-box .panel-heading diff --git a/app/views/question/show.js.erb b/app/views/question/show.js.erb new file mode 100644 index 00000000..bcaf826e --- /dev/null +++ b/app/views/question/show.js.erb @@ -0,0 +1,8 @@ +$('#answers').append('<% @answers.each do |answer| + %><%= j render 'shared/answerbox', a: answer, show_question: false +%><% end %>'); +<% if @answers.next_page %> + $('#pagination').html('<%= j will_paginate @answers, renderer: BootstrapPagination::Rails, page_links: false %>'); +<% else %> + $('#pagination, #load-more-btn').remove(); +<% end %> \ No newline at end of file