In a Rails 3.2 index view I am rendering two partials.
<%= render :partial => 'users/user', :locals => {:users => @happy_users} %>
<%= render :partial => 'users/user', :locals => {:users => @sad_users} %>
and in the partial
<% users.each do |user| %>
Show some fields
<% end %>
<%= will_paginate users %>
The pagination is not working.
If I alter will_paginate to take an instance variable, pagination works (but the wrong collection)
<%= will_paginate @users %>
How can I pass locals to will_paginate when the partial is called?
(I realize that I’ll also need to pass :param_name to get this working with two collections. For now I’m just trying to get one instance working.)
The partial is rendered via index.js.erb
$(".ajaxable-users").html('<%= escape_javascript(render("users/user")) %>');
And the controller looks like
def index
@users = User.scoped.paginate(:page => params[:page], :per_page => 5)
@happy_users = User.happy_scope.paginate(:page => params[:page], :per_page => 5)
@sad_users = User.happy_scope.paginate(:page => params[:page], :per_page => 5)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @users }
format.json { render :json => @users }
format.js
end
end
Thanks for any ideas.
so, to set controller instance vars depending on request mood parameter
.
make sure we dispatch the right locals to partials:
make partial mood sensitive allowing for distinct div tag id. also put mood in request url.
this js allows for refreshing different collections in different divs depending on which pagination link was pressed.
For unobtrusive ajax links for pagination you need.
Ruby – Rails 3 – AJAXinate Paginate and sorting
Note that the solution should even work if javascript is not enabled and we fall back on html. In this case both sections should be redisplayed allowing for different pages.
MODIFIED CODE TO ALSO HANDLE GRACEFUL JS FALLBACK TO HTML
controller
forward new view vars to partial local
we only need to pass this here to not get undefined method in partial.
this will then persist happy pages in param if sad page link is clicked, and vica versa.