2012-02-27 66 views
0

我想創建一個無盡的頁面。Rails 3.0如何創建無盡的頁面?

本教程後:http://railscasts.com/episodes/114-endless-page?view=comments

在我的控制,我有:

def index 
@konkurrencer = Konkurrencer.find(:all).paginate(:page => params[:page], :per_page => 2) 
    respond_to do |format| 
     format.html 
     format.js { render :rjs => @konkurrencer } 
     end 
end 

在我看來,我有:

<% @konkurrencer.each do |kon| %> 
<%= render :partial => 'konkurrencers/konkurrencer', :locals => { :kon => kon } %> 
<% end %> 

我index.js.rjs:

page.insert_html :bottom, :konkurrencer, :partial => 'konkurrencers/konkurrencer' 
if @konkurrencer.total_pages > @konkurrencer.current_page 
    page.call 'checkScroll' 
else 
    page[:loading].hide 
end 

在我的頭我此javascript:

<%= javascript_include_tag 'jquery', 'endless' %> 

而且endless.js:

var currentPage = 1; 

function checkScroll() { 
    if (nearBottomOfPage()) { 
    currentPage++; 
    new Ajax.Request('/konkurrencer.js?page=' + currentPage, {asynchronous:true, evalScripts:true, method:'get'}); 
    } else { 
    setTimeout("checkScroll()", 250); 
    } 
} 

function nearBottomOfPage() { 
    return scrollDistanceFromBottom() < 150; 
} 

function scrollDistanceFromBottom(argument) { 
    return pageHeight() - (window.pageYOffset + self.innerHeight); 
} 

function pageHeight() { 
    return Math.max(document.body.scrollHeight, document.body.offsetHeight); 
} 

document.observe('dom:loaded', checkScroll); 

有做什麼的沒有Ajax調用。應該將index.js.rjs移至index.js.erb?還是因爲我不包含默認的JavaScript?

回答

1

不,我認爲你的塊的問題迴應js。如果你想讓它呈現index.js.rjs,那麼你只是應該能夠說:

def index 
    @konkurrencer = Konkurrencer.find(:all).paginate(:page => params[:page], :per_page => 2) 
    respond_to do |format| 
    format.html 
    format.js 
    end 
end 

這應該尋找一個名爲index.js.SOMEFORMAT文件。我不相信你的:rjs => @koncurrencer實際上是在工作,我敢打賭,如果你看着你的瀏覽器流量,你會看到服務器因錯誤返回500。