Rails beginner
Rails beginner

Reputation: 14514

Rails 3.0 how to create endless page?

I am trying to create a endless page.

After this tutorial: http://railscasts.com/episodes/114-endless-page?view=comments

In my controller I have:

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

In my view I have:

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

My 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

In my header I this javascript:

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

And the 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);

There is no ajax call made or anything. Should move the index.js.rjs to index.js.erb? or is it because I don´t include default javascript?

Upvotes: 0

Views: 259

Answers (1)

TheDelChop
TheDelChop

Reputation: 7998

No, I think the problem your block for responding to js. If you want it to render index.js.rjs, then you just should be able to say:

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

That should look for a file called index.js.SOMEFORMAT. I dont' believe your :rjs => @koncurrencer is actually working, I bet if you looked at your browser traffic, you'd just see the server return a 500 because of the error.

Upvotes: 1

Related Questions