2012-04-04 47 views
1

上午在我的項目中使用kaminari ajax分頁。它工作正常,但內容顯示頁面中的頁面次數。例如,如果每頁的項目數是7,則它顯示相同內容的7倍。什麼我做的是kaminari ajax分頁問題

在product_details控制器

def index 
    @products=ProductDetail.where("department_id = ? and category_id = ?",1, 1).page(params[:page]).per(15) 
    end 

在product_details/index.html.erb

<div id="product_details"> 
    <%= render @products %> 
</div> 
<div id="paginator"> 
    <%= paginate @products, :remote=>true %> 
</div> 

在product_details/index.js.erb的

$('#product_details').html('<%= escape_javascript render (@products) %>'); 
$('#paginator').html('<%= escape_javascript(paginate(@products, :remote=>true).to_s)%>'); 

在product_details /_product_detail.html.erb

<div id="product_list">    
    <% @products.each do | product | %> 
     <div class="product_container">  
      <div class="product_box" > 
       <div id="future_image_container"> 
        <div class="image_block" > 
         <%= image_tag(product.image_path, :alt => "product image", :height=>"215px") %> 
        </div> 
        <span id="future_price" style="text-decoration: line-through; color: #9e9c9c;"> 
         <span style="color: black;"> 
          <%= "$#{product.price}" %> 
         </span> 
        </span> 
        <div id="circle"> 
         <p> 
          <% if(product.discount > 0) %> 
           <% new_price=((2.0*((product.price*product.discount)/100)).round)/2.0 %> 
          <% else %> 
           <% new_price=product.price %> 
          <% end %> 
          <%= "$#{new_price}"%> 
         </p> 
        </div> 
       </div> 
       <p class="future_product_name"> 
        <%= product.name %> 
       </p> 
       <% @brands=Brand.where("id=?",product.brand_id) 
        @brands.each do |brand| 
         @brandname=brand.name 
        end 
       %> 
       <p class="future_product_name"> 
        <%= "Brand : #{@brandname}" %> 
       </p> 
      </div> 
     </div> 
    <% end %> 
</div> 

請幫我解決這個問題

回答

2

我注意到,當我使用<%=渲染@products%>我有多少項目每頁採取了一些它repeates倍。因此,我通過下面的代碼解決了這個:

在product_details控制器

def index 
    @products=ProductDetail.where("department_id = ? and category_id = ?",1, 1).page(params[:page]).per(15) 
    end 

在product_details/home.html.erb

<div id="product_details"> 
    <%= render 'index' %> 
</div> 

在product_details/index.js.erb的

$('#product_details').html('<%= escape_javascript render ('index') %>'); 

In product_details/_index.html.erb

<div id="product_list"> 
    <%= paginate @products, :remote=>true %>     
    <% @products.each do | product | %> 
     <div class="product_container">  
      <div class="product_box" > 
       <div id="future_image_container"> 
        <div class="image_block" > 
         <%= image_tag(product.image_path, :alt => "product image", :height=>"215px") %> 
        </div> 
        <span id="future_price" style="text-decoration: line-through; color: #9e9c9c;"> 
         <span style="color: black;"> 
          <%= "$#{product.price}" %> 
         </span> 
        </span> 
        <div id="circle"> 
         <p> 
          <% if(product.discount > 0) %> 
           <% new_price=((2.0*((product.price*product.discount)/100)).round)/2.0 %> 
          <% else %> 
           <% new_price=product.price %> 
          <% end %> 
          <%= "$#{new_price}"%> 
         </p> 
        </div> 
       </div> 
       <p class="future_product_name"> 
        <%= product.name %> 
       </p> 
       <% @brands=Brand.where("id=?",product.brand_id) 
        @brands.each do |brand| 
         @brandname=brand.name 
        end 
       %> 
       <p class="future_product_name"> 
        <%= "Brand : #{@brandname}" %> 
       </p> 
      </div> 
     </div> 
    <% end %> 
</div> 

現在沒有產品重複和ajax分頁也工作正常