2012-12-03 79 views
0

我已經得到了一個任務做pagination.When我這樣做分頁我已經得到了這些錯誤分頁在Ruby on Rails的

NoMethodError in ProductsController#index 

undefined method `page' for []:ActiveRecord::Relation 

Rails.root: /home/nithinv/store 
Application Trace | Framework Trace | Full Trace 

app/controllers/products_controller.rb:4:in `index' 

這是我的控制器

def index 

     @products = Product.order(:name).page(params[:page]).per(2) 

     respond_to do |format| 
     format.html #index.html.erb 
     format.json { render :json=> @products } 
     end 
    end 

這是我的索引。 html.erb

<% title "Products" %> 

<%= paginate @products %> 

<% for product in @products %> 
    <div class="product"> 
    <h2><%= link_to product.name, product %></h2> 
    <div class="details"> 
     <%= number_to_currency(product.price) %> | 
     Released <%= product.released_at.strftime("%B %e, %Y") %> 
    </div> 
    </div> 
<% end %> 

<p><%= link_to "New Product", new_product_path %></p> 

我該如何解決這個問題?

回答

0

參考this

我想你忘了添加下面一行在你的gemfile

gem 'kaminari' 

,然後捆綁安裝

0

我看來,這個問題與雷寶石配置做(不你的代碼)。我建議你這樣做快速檢查:

  1. 運行bundle install
  2. 重新啓動服務器(因爲變化可能不會被加載)
  3. 檢查你的Gemfile因爲,雷寶石可以被放錯了地方(下:測試組)

有點顯而易見,但有些時候我還發現自己忘記了一些基本檢查後會浪費時間。