Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8005265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:10:49+00:00 2026-06-04T17:10:49+00:00

In a Rails 3.2 index view I am rendering two partials. <%= render :partial

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:10:51+00:00Added an answer on June 4, 2026 at 5:10 pm
    1. you need to make sure the controller knows which page link of which listing you press. we do this by passing a param to will_paginate page link generator.
    2. you need to make sure your js updates the correct html tag when refreshing the pagination of either listing. we do this by wrapping the pagination in a mood-dependent 🙂 div tag.
    3. since we share the partial we need to make sure mood as well as correct collection is set in controller and passed to partial as local from index views (both html and js).
    4. finally make sure your ajax remote picks up after you redraw pagination (this is a bonus)

    so, to set controller instance vars depending on request mood parameter

    • note that this code also spares you some db calls, since if you just pagintae sad users you do not need to retrieve happy ones or all).
    • I ommit @users for simplicity, its never used
    • I suspect happy scope for sad users is only 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 dispatch the right locals to partials:

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

    make partial mood sensitive allowing for distinct div tag id. also put mood in request url.

    # 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 for refreshing different collections in different divs depending on which pagination link was pressed.

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

    For unobtrusive ajax links for pagination you need.
    Ruby – Rails 3 – AJAXinate Paginate and sorting

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

    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

    # 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
    

    forward new view vars 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 only need to pass this here to not get 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 })) %>
    ');
    

    this will then persist happy pages in param if sad page link is clicked, and vica 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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my rails view(index.html.erb), i have following structure <div> <%= render :partial => create
Two models (Rails 2.3.8): User; username & disabled properties; User has_one :profile Profile; full_name
I have a problem, trying to render partials in ruby on rails with the
My destroy link is not working. My index view: <div id=konkurrancer><%= render 'konkurrencer', :remote
They are two users: User - logged in user Guest - not logged in
I have a index view in a Rails 3 project where several line items
In a Rails index view I'm implementing ajax pagination, more or less following this
Running the command generates new rails projects: $ rails generate controller home index The
I am using rails beta 3 and I have a erb page named index.html.erb
In my Rails application, I would like to route /product/productname1, /product/productname2, etc. to ProductController::index().

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.