Question

I am following the Railcast tutorial on endless page. I am receiving a Argument Error The @users variable appears to be empty. Did you forget to pass the collection object for will_paginate?

The javascript has been modified so you have to click a read more link to get more results. So the pagination will work by click instead of scrolling.

The error points to the line <%= will_paginate @questions %>

Questions Controller:

      def index
#Before gem added it was `@questions = Question.all`
        @questions = Question.order("question").page(params[:users]).per_page(2)    
        respond_with(@questions)
    end

Users Controller:

  def profile
    @profile = User.profile
    @user = User.find(params[:id]) 
    @question = Question.where(recipient_id: params[:id]).first
  end

  def show
    @user = User.find(params[:id])
    @question = Question.where(recipient_id: params[:id])
  end

View:

<% @question.select(&:answer?).each do |question| %>
Question: <%= question.question %>
<br>
Answer: <%= question.answer %><br><br>

<%= will_paginate @questions %>
<% end %>

Questions.js.coffee:

jQuery ->
  if $('.pagination').length
    $('#append_and_paginate').prepend('<a id="append_more_results" href="javascript:void(0);">Show more questions</a>');
    $('#append_more_results').click ->
      url = $('.pagination .next_page').attr('href')
      if url
        $('.pagination').text('Fetching more questions...')
        $.getScript(url)

Index.js.erb:

$('#questions').append('<%= j render(@questions) %>');
<% if @questions.next_page %>
  $('.pagination').replaceWith('<%= j will_paginate(@questions) %>');
<% else %>
  $('#append_and_paginate').remove();
<% end %>
<% sleep 1 %>
Was it helpful?

Solution

Replace

    @questions = Question.order("question").page(params[:users]).per_page(2)    

With

     @questions = Question.order("question").paginate(:page => params[:page], :per_page => 2)    

EDIT

Update the User model as below:

class User < ActiveRecord::Base

  has_many :sender_questions, :class_name => 'Question', :foreign_key => 'sender_id' 
  has_many :recipient_questions, :class_name => 'Question', :foreign_key => 'recipient_id'

end

Update the UsersController#show action as below:

def show
  @user = User.find(params[:id]) 
  @question = @user. recipient_questions.paginate(page: params[:page])
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top