How to display two grouped and ayamach collections in one view?

In the Rails 3.2 index view, I handle two partial parts.

<%= render :partial => 'users/user', :locals => {:users => @happy_users} %> <%= render :partial => 'users/user', :locals => {:users => @sad_users} %> 

and in partial

 <% users.each do |user| %> Show some fields <% end %> <%= will_paginate users %> 

Pagination does not work.

If I modify will_paginate to take an instance variable, pagination is done (but the collection is wrong)

 <%= will_paginate @users %> 

How can I pass locals to_paginate when calling partial?

(I understand that I also need to pass :param_name for this to work with two collections. At the moment, I'm just trying to get one instance to work.)

Partial rendering is done using 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.

+8
ajax ruby-on-rails will-paginate
source share
3 answers
  • you need to make sure that the controller knows which link to the page that lists . we do this by passing param to the will_paginate page link generator.
  • you need to make sure your js updates the correct html tag when updating the pagination of any of them. we do this by wrapping the pagination in a mood-dependent :) div tag.
  • since we are separating the particle, we need to make sure that the mood, as well as the correct collection, is set in the controller and partially transmitted as local from the index representations (both html and js).
  • finally make sure your ajax remote is removed after you redraw the pagination (this is a bonus)

therefore, to install instances of the controller instance, depending on the request mood parameter

  • note that this code also saves you some db calls, because if you are just pagintae to sad users, you don't need to get happy or all).
  • I omit @users for simplicity, never used
  • I suspect that happy opportunities for sad users are just a typo

.

 # controller def index @mood = params[:mood] @mood_users = @happy_users = User.happy_scope.paginate(:page => params[:page], :per_page => 5) if @mood.blank? || @mood == 'happy' @mood_users = @sad_users = User.happy_scope.paginate(:page => params[:page], :per_page => 5) if @mood.blank? || @mood == 'sad' respond_to do |format| format.html # index.html.erb format.xml { render :xml => @users } format.json { render :json => @users } format.js end end 

make sure we ship the correct local parts in parts:

 # index.html.erb <%= render :partial => 'users/user', :locals => {:users => @happy_users, :mood => 'happy' } %> <%= render :partial => 'users/user', :locals => {:users => @sad_users, :mood => 'sad' } %> 

make a partial mood-sensitive parameter that allows you to use a separate div tag identifier. also add mood in url request.

 # users/user partial <div id='<%= "#{mood || ''}users"%>'> <% users.each do |user| %> Show some fields <% end %> <%= will_paginate users, :params => { :mood => mood } %> </div> 

this js allows you to update different collections in different divs, depending on which link was clicked.

 # index.js.erb $("#<%= @mood %>users").html(' <%= escape_javascript(render(partial: "users/user", locals: { users: @mood_users, mood: @mood })) %> '); 

For unobtrusive ajax pagination links you need. Ruby - Rails 3 - AJAXinate Paginate and Sort

 # in a generic js $(document).ready(function () { $(".pagination").find("a").livequery(function () { $(this).attr("data-remote", true); }); }); 

Please note that the solution should even work if javascript is not enabled and we return to html. In this case, both sections must be redrawn, which allows you to use different pages.

MODIFIED CODE THAT ALSO REVERSES GRAPHIC JS FALLBACK IN HTML

controller

 # controller def index @mood = params[:mood] @happy_page = @mood == 'happy' ? params[:page] : params[:happy_page] @sad_page = @mood == 'sad' ? params[:page] : params[:sad_page] @mood_users = @happy_users = User.happy_scope.paginate(:page => @happy_page, :per_page => 5) if !request.xhr? || @mood == 'happy' @mood_users = @sad_users = User.happy_scope.paginate(:page => @sad_page, :per_page => 5) if !request.xhr? || @mood == 'sad' @mood_users = @users = User.scoped.paginate(:page => params[:page], :per_page => 5) if @mood.blank? respond_to do |format| format.html # index.html.erb format.xml { render :xml => @users } format.json { render :json => @users } format.js end end 

redirect new views to partial local

 # index.html.erb <%= render :partial => 'users/user', :locals => {:users => @happy_users, :mood => 'happy', :happy_page => @happy_page, :sad_page => @sad_page } %> <%= render :partial => 'users/user', :locals => {:users => @sad_users, :mood => 'sad', :happy_page => @happy_page, :sad_page => @sad_page } %> 

we need to pass this here so as not to get the undefined method in partial.

 # index.js.erb (fixed HTML syntax) $("#<%= @mood %>users").html(' <%= escape_javascript(render(partial: "users/user", locals: { users: @mood_users, mood: @mood, :happy_page => @happy_page, :sad_page => @sad_page })) %> '); 

then happy pages will be saved in the parameter if a sad link to the page is pressed, and vice versa.

 # users/user partial <div id='<%= "#{mood || ''}users"%>'> <% users.each do |user| %> Show some fields <% end %> <%= will_paginate users, :params => { :mood => mood, :happy_page => happy_page, :sad_page => sad_page %> </div> 
+4
source share

First, create two different methods for each list in the controller, for example fill_list1 and fill_list2, regardless of the method for creating the main page, for example index

Each list method should display partial without layout, e.g.

 render :partial=>'fill_list1', :layout => false 

The main page should first call partial views.

when you click on the paginate control you have to hack click to call ajax the following list of pagination elements

 $('list1 a').live('click', function(event){ event.preventDefault(); $.ajax( {url:URLFORCALLMETHODLIST, success: function(content) { DIVLOCATIONLIST.html(content); }, async:false, }); }); 
0
source share

Try passing the correct locales to index.html.erb :

 $(".ajaxable-users").html(' <%= escape_javascript(render("users/user", locals: { users: @users })) %> '); 
-one
source share

All Articles