2014-03-21 3 views
0

Я следую руководству Railcast на бесконечной странице. Я получаю сообщение об ошибке The @users variable appears to be empty. Did you forget to pass the collection object for will_paginate?Пользовательская переменная кажется пустой для 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

Заменить

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

С

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

EDIT

Обновление модели Пользователь, как показано ниже:

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 # показать действие, как показано ниже:

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

Та же ошибка остается, к сожалению. – pwz2000

+0

Можете ли вы поделиться ошибкой stacktrace в вопросе. –

+1

Похоже, вы вызываете 'will_paginate @ users' в одном из видов, а @users не задано. Было бы проще решить вашу проблему, если вы предоставите stacktace в вопросе. –

Смежные вопросы