Question

This is the controller I have writter for Orders.

 def index
    if current_user.has_role? :admin
      @orders = Order.all.paginate(:page => params[:page], :per_page => 5)
    elsif current_user.has_role? :customer
      @orders = Order.where(:email => current_user.email).paginate(:page => params[:page], :per_page => 5)
    elsif current_user.has_role? :white_label
      @orders = Order.where(:performer_id => (Performer.where(:white_label_id => current_user.white_label.id))).paginate(:page => params[:page], :per_page => 5)
    else
      @orders = current_user.performer.orders.paginate(:page => params[:page], :per_page => 5)
    end
    @custom_video = CustomVideo.new
  end

As you see I have used will_paginate gem to do pagination. This is the view page where I am doing pagination.

<div class="container">
  <div class="span 8">
            <% if current_user.has_role? :admin %>
            <%role =1%>
          <%elsif current_user.has_role? :performer %>
            <%role =2%>
          <% else %>
            <%role =3%>          
          <% end %>  
      <h3>Awaiting Upload</h3>
        <table id="order_table" class="table table-hover">
          <thead>
            <tr>
            <th>id</th>
              <th>Location</th>
              <th>Performer</th>
              <th>Duration</th>
              <th>Quality</th>
              <th>Delivery</th>
              <th>Category</th>
              <th>Description</th>
              <th>Total</th>
              <% if role==2 %>
              <th>Select a video to upload for the order</th>
              <%end %>
              <th></th>
              <th></th>
              <th></th>
            </tr>
          </thead>
          <tbody>
            <% @orders.each do |order| %>
           <% if order.delivery_time_id=1%>
            <% var1=14 %>
            <% else %>
              <% var1=7 %>
            <% end %>
            <% if !CustomVideo.find_by_order_id(order.id) and Time.now <= order.created_at.to_date + var1.days%>

             <tr>
                <td> <%= order.id%></td>
                <td><% if order.location %>
                  <%= order.location.name %>
                <% end %></td>
                <td><% if order.performer %>
                  <%= order.performer.first_name %>
                <% end %></td>
                <td><% if order.duration %>
                <%= order.duration.time %>
                <% end %></td>
                <td><% if order.quality %>
                <%= order.quality.name %>
                <% end %></td>
                <td><% if order.delivery_time %>
                <%= order.delivery_time.duration %>
                <% end %></td>
                <td><% if order.clip_category %>
                <%= order.clip_category.name %>
                <% end %></td>
                <td><% if order.description %>
                <%= order.description %>
                <% end %></td>
                <td><%= order.total %></td>

                <% if role==2 %>
                  <td>
                    <%= simple_form_for(@custom_video, :html => { :multipart => true, :id => "fileupload"  }) do |f| %>
                    <%= f.error_notification %>
                    <%= f.file_field :path%><br/><br>
                    <%= f.hidden_field :order_id,:value => order.id %>
                    <%=f.button :submit, :value=>"Save", :class=>"btn btn-success" %>
                    <% end %>
                    <script id="template-upload" type="text/x-tmpl">
<div class="upload">
  {%=o.name%}
  <div class="progress"><div class="bar" style="width: 0%"></div></div>
</div>
</script>
                    <%end%>
                  </td>
                  <td>
                  <% if role==1 %>
                  <%=form_tag({controller: "orders", action: "refund"}, method: "post") do%>
                  <%= hidden_field_tag(:id, order.id) %>
                  <%= submit_tag ("Refund"),:class => "btn btn-success download" %>
                  <% end %>
                <% end %>
                </td>
                <% if can? :show, @order %>
                  <td><%= link_to 'Show', order %></td>
                <% end %>
                <% if can? :update, @order %>
                  <td><%= link_to 'Edit', edit_order_path(order) %></td>
                <% end %>
                <% if can? :destroy, @order %>
                  <td><%= link_to 'Destroy', order, method: :delete, data: { confirm: 'Are you sure?' } %>
                  </td>
                <%else %>
                <% next %>
                <%end %>
                <% end -%>

            <% end %>
                                    <%= will_paginate @orders, :param_name => 'awaiting_orders' %>

          </tbody>
        </table><br/>
        <h3>Completed Orders</h3>
        <table class="table table-hover" id="order_table">
            <thead>
              <tr>
                <th>id</th>
                <th>Location</th>
                <th>Performer</th>
                <th>Duration</th>
                <th>Quality</th>
                <th>Delivery</th>
                <th>Category</th>
                <th></th>
                <th></th>
              </tr>
          </thead>
          <tbody>
            <% @orders.each do |order| %>
            <% if order.delivery_time_id=1%>
            <% var1=14 %>
            <% else %>
              <% var1=7 %>
            <% end %>
            <% if CustomVideo.find_by_order_id(order.id) and Time.now <= order.created_at.to_date+var1.days%>
             <tr>
             <td> <%= order.id%></td>
                <td><%= order.location.name %></td>
                <td><%= order.performer.first_name %></td>
                <td><%= order.duration.time %></td>
                <td><%= order.quality.name %></td>
                <td><%= order.delivery_time.duration %></td>
                <td><%= order.clip_category.name %></td>
                <td>
                  <% if role==1 %>
                  <%=form_tag({controller: "orders", action: "refund"}, method: "post") do%>
                  <%= hidden_field_tag(:id, order.id) %>
                  <%= submit_tag ("Refund"),:class => "btn btn-success download" %>
                  <% end %>
                <% end %>
                </td>
                <% if can? :show, @order %>
                  <td><%= link_to 'Show', order %></td>
                <% end %>
                <% if can? :update, @order %>
                  <td><%= link_to 'Edit', edit_order_path(order) %></td>
                 <% end %>
                <% if can? :destroy, @order %>
                  <td><%= link_to 'Destroy', order, method: :delete, data: { confirm: 'Are you sure?' } %></td>
                  <%else %>
                <% next %>
              <%end %>
                <% end -%>
              </tr>
            <% end %>
                                    <%= will_paginate @orders %>

          </tbody>

          </table><br/>
          <h3>Expired Orders</h3>
        <table class="table table-hover" id="order_table">
            <thead>
              <tr>
              <th> id</th>
                <th>Location</th>
                <th>Performer</th>
                <th>Duration</th>
                <th>Quality</th>
                <th>Delivery</th>
                <th>Category</th>
                <th></th>
                <th></th>
              </tr>
          </thead>
          <tbody>
            <% @orders.each do |order| %>
            <% if order.delivery_time_id=1%>
            <% var1=14 %>
            <% else %>
              <% var1=7 %>
            <% end %>
            <% if  Time.now > order.created_at.to_date+var1.days%>
             <tr>
             <td> <%= order.id%></td>
                <td><%= order.location.name %></td>
                <td><%= order.performer.first_name %></td>
                <td><%= order.duration.time %></td>
                <td><%= order.quality.name %></td>
                <td><%= order.delivery_time.duration %></td>
                <td><%= order.clip_category.name %></td>
                <% if can? :show, @order %>
                  <td><%= link_to 'Show', order %></td>
                <% end %>
                <% if can? :update, @order %>
                  <td><%= link_to 'Edit', edit_order_path(order) %></td>
                 <% end %>
                <% if can? :destroy, @order %>
                  <td><%= link_to 'Destroy', order, method: :delete, data: { confirm: 'Are you sure?' } %></td>
                  <%else %>
                <% next %>
              <%end %>
                <% end -%>
              </tr>
            <% end %>
                                    <%= will_paginate @orders %>

          </tbody>
        </table>
        <br>
        <h3>Orders Refunded</h3>
        <table class="table table-hover" id="order_table">
            <thead>
              <tr>
              <th> id</th>
                <th>Location</th>
                <th>Performer</th>
                <th>Duration</th>
                <th>Quality</th>
                <th>Delivery</th>
                <th>Category</th>
                <th></th>
                <th></th>
              </tr>
          </thead>
          <tbody>
            <% @orders.each do |order| %>
            <% if  order.refunded %>
             <tr>
             <td> <%= order.id%></td>
                <td><%= order.location.name %></td>
                <td><%= order.performer.first_name %></td>
                <td><%= order.duration.time %></td>
                <td><%= order.quality.name %></td>
                <td><%= order.delivery_time.duration %></td>
                <td><%= order.clip_category.name %></td>
                <% if can? :show, @order %>
                  <td><%= link_to 'Show', order %></td>
                <% end %>
                <% if can? :destroy, @order %>
                  <td><%= link_to 'Destroy', order, method: :delete, data: { confirm: 'Are you sure?' } %></td>
                  <%else %>
                <% next %>
              <%end %>
                <% end -%>
              </tr>
            <% end %>
                                    <%= will_paginate @orders %>

          </tbody>
        </table>
        <br>

  </div>
</div>
<%# link_to 'New Order', new_order_path %>

The problem in this is that. If I click the second page for the first table, it is going to the second for the next table too. I have tried giving the :param_name => 'questions_page' suggested here. But the trouble here is that, I am not using different instance variable for the tables but the same one. How do I solve this?>

Was it helpful?

Solution

You've almost answered your own question. You need to be using separate instance variables for each table if you want them to behave separately.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top