will_paginate在相同的页面中为同一个实例变量但三个不同的显示

时间:2013-10-04 14:14:42

标签: ruby-on-rails ruby-on-rails-4 will-paginate

这是我为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

如您所见,我使用will_paginate gem进行分页。这是我正在进行分页的视图页面。

<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 %>

问题在于此。如果我单击第一个表的第二页,它也将转到下一个表的第二页。我试过给出:param_name =&gt; 'questions_page'建议here。但问题是,我没有为表使用不同的实例变量,而是使用相同的实例变量。我该如何解决这个问题?&gt;

1 个答案:

答案 0 :(得分:0)

你几乎回答了自己的问题。如果希望它们单独运行,则需要为每个表使用单独的实例变量。