简体   繁体   English

Rails-使用.each do时无法使用will_paginate gem

[英]Rails - will_paginate gem not working when using .each do

EDIT: I've altered the code to now read: 编辑:我已更改代码以读取:

View code: 查看代码:

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

Controller Code: 控制器代码:

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

Routes: 路线:

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'

The problem is now that I'm getting the error: 现在的问题是我遇到了错误:

The @orders variable appears to be empty. @orders变量似乎为空。 Did you forget to pass the collection object for will_paginate? 您是否忘记为will_paginate传递收集对象?

whenever I click on a "Show" link in the list of orders in the view. 每当我单击视图中的订单列表中的“显示”链接时。 Tried restarting the server just in case, but can't get it working :S! 尝试重新启动服务器以防万一,但无法使其工作:S!

It should be 它应该是

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

not

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

Pls check this code 请检查此代码

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

in view 鉴于

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM