Rails jQuery pageless problem
Here's the deal,
I'm trying to solve why pageless (infinite scroll) isn't rendering. Using this jQuery Plugin
I have my index page of products with pagination by will_paginate:
def index
@products = Product.find(:all, :order => "position DESC").paginate(:per_page => 3, :page => params[:page])
if request.xhr?
render :partial => 'shared/products', :object => @products
end
end
My index page is calling the products partial
<%= render "shared/products", :object => @products %>
Which in turn is passing the collection onto a singular product partial
<%= render 'shared/product', :collection => @products %>
And calling pageless on it if javascript is enabled (otherwise will_paginate steps in.
<%= pageless(@products.total_pages, products_path) %>
-----------
def pageless(total_pages, url=nil, container=nil)
opts = {
:totalPages => total_pages,
:url => url,
:loaderMsg => 'Loading more results'
}
container && opts[:container] ||= container
javascript_tag("$('#results').pageless(#{opts.to_json});")
end
And finally here is the code for the singular product partial
<% @products.each do |product| %>
<li id="product_<%= product.id %>">
<%= link_to product_image(product), product %>
</li>
<% end %>
Now when I load the page, there are no js errors, I can see the pages getting loaded in my server logs and yet nothing is rendering. Everything seems to work perfectly and yet it doesn't render. Any help wo开发者_StackOverflow中文版uld be appreciated.
-Scott
The products weren't being appended to the html. Turned out to be a class/id issue in the html. For anyone trying to debug something similar, see Jonathan Tran's comment on the question.
i changed few things to make it work for rails3:
index.html.erb
<%= render :partial => 'blah/fooPartial' , :locals => { :foo => @foo} %>
<%= will_paginate(@foo) %>
<%= pageless(@foo.total_pages, @path ) %>
My FooController:
def index
@foo = Foo.paginate(:page => params[:page], :per_page => 10).
order("created_at DESC")
respond_to do |format|
format.js
format.html # index.html.erb
end
end
My index.js.erb
$('.row').last().after("<%= escape_javascript( render :partial => 'blah/fooPartial' , :locals => { :foo => @foo}).html_safe %>");
Also Changed the ajax datatype at the very end from 'html' to 'script' in jquery.pageless.js as suggested by ahuang
Hope this helps!!!cheers!!!
精彩评论