1

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

查看代码:

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

4

2 回答 2

1

它应该是

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

不是

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

于 2012-06-19T11:48:20.693 回答
0

请检查此代码

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

在视野中

<% @orders.each do |order| %>
于 2012-06-19T12:05:50.060 回答