我有一个任务要做分页。当我做分页时,我遇到了这些错误
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
这是我的 index.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>
我怎么解决这个问题?