Rails - 使用.each时,will_paginate gem无法正常工作

时间:2012-06-19 11:24:44

标签: ruby-on-rails will-paginate

编辑:我已将代码更改为现在读取:

查看代码:

<h3>Your Orders:</h3>
<table class="order-items">
<th>Date</th>
<th>Order No.</th>
<th>View Order</th>
<% @orders.each do |order| %>
    <tr>
        <td><%= order.created_at.strftime("%d %b. %Y") %></td>
        <td><%= order.id %></td>
        <td><%= link_to 'Show',  order_path(order) %></td>
    </tr>
<% end %>   
</table>

<p><%= will_paginate @orders %></p>

控制器代码:

@customer = Customer.find(params[:id])
@orders = @customer.orders.paginate page: params[:page], order: 'id desc',
  per_page: 5

路线:

resources :orders

resources :customers

get 'admin' => 'admin#index'

get 'account' => 'customers#show'

match '/account', :to => 'customers#show'

match'/signup', :to => 'customers#new'

controller :sessions do
  get  'login' => :new
  post 'login' => :create
  delete 'logout' => :destroy
end


resources :users

resources :line_items

resources :carts

get "store/index"

resources :products do
  get :who_bought, on: :member
end

resources :line_items do
  put 'decrease', on: :member
  put 'increase', on: :member
end

root to: 'store#index', as: 'store'

问题是我现在收到错误:

@orders变量似乎为空。你忘记了为will_paginate传递集合对象吗?

每当我点击视图中订单列表中的“显示”链接时。尝试重新启动服务器以防万一,但无法使其正常工作:S!

2 个答案:

答案 0 :(得分:1)

应该是

<% @orders.each do |order| %>

<% @customer.orders.each do |order| %>

答案 1 :(得分:0)

请检查此代码

@customer = Customer.find(params[:id])
@orders = @customer.orders.paginate page: params[:page], order: 'title',per_page: 5

在视图中

<% @orders.each do |order| %>