【问题标题】:undefined method `total_pages' for #<Array:0x1041df24>#<Array:0x1041df24> 的未定义方法“total_pages”
【发布时间】:2018-10-08 05:40:36
【问题描述】:

我正在尝试对数组进行分页,但无法这样做。请帮忙

以下是我的控制器代码:

require 'will_paginate/array'
class AccountsController < ApplicationController
        def children
            @account = @rest_resource
            @children = []
      User.find_all_by_parentId(@account.user.id).paginate(:page 
      => params[:page], :per_page => 10).each { |eu|
            u = eu.user
            ent = Table2.find(u.login)
            @children << ent unless ent.nil?
            }
        end
end

查看文件 -> children.html.erb:

<%= will_paginate @children %>
<table>
<%= render :partial => 'account', :collection => @children %>
</table>

部分视图->_account.html.erb:

<tr class="<%= cycle('even', 'odd') %> <% if account.user.is_test? %>Test<% end %>">
    <td><strong><%= link_to h(account.login), account_path(account) %></strong></td>
    <td><% if account.user.test? %>Test <% end %><%= account.attr2 %></td>
    <td>
      <strong><%= account.companyName %></strong><br/>
      <%= account.contact %> - <%= account.Email %> - <%= account.Mobile %>
    </td>
    <td><%=h account.Status %></td>
</tr>

这不起作用。它说:

undefined method `total_pages' for #<Array:0x1041df24>

Rails 版本:

Rails 3.2.13

JRuby 版本:

jruby 9.1.17.0 (2.3.3)

我的应用正在使用:

will_paginate (3.0.4)

请有人帮忙解决这个问题

【问题讨论】:

  • 请任何人帮忙。
  • 能否指定使用total_pages方法的代码

标签: ruby-on-rails-3 jruby will-paginate


【解决方案1】:

您将@children 初始化为一个数组,并将其传递给 will_paginate 所以它会抛出一个错误...尝试像传递一样

will_paginate @children[0]

如果您想传递该数组中的每个实例,请使用

@children.each

方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-04
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多