对于will_paginate,users变量似乎为空

时间:2014-03-21 19:58:02

标签: ruby-on-rails pagination will-paginate

我正在无尽的页面上关注Railcast教程。我收到了参数错误The @users variable appears to be empty. Did you forget to pass the collection object for will_paginate?

javascript已被修改,因此您必须点击阅读更多链接才能获得更多结果。因此,分页将通过单击而不是滚动来工作。

错误指向第<%= will_paginate @questions %>

问题主持人:

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

用户控制器:

  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

查看:

<% @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 %>

1 个答案:

答案 0 :(得分:1)

替换

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

使用

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

修改

更新用户模型,如下所示:

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

更新UsersController#show action,如下所示:

def show
  @user = User.find(params[:id]) 
  @question = @user. recipient_questions.paginate(page: params[:page])
end