与Kaminari宝石有麻烦

时间:2014-01-13 18:03:43

标签: ruby-on-rails arrays pagination gem kaminari

我正在尝试使用Kaminari Gem对多个表进行分页。我试图通过在我的应用程序控制器中生成一个数组来实现这一点,如下所示:

@paginate = %w(errors messages subscribers).page(params[:page]).per(15)

然后我采用了@paginate并将其用于我的一个部分而不是渲染表格,如下所示:

<%= paginate @paginate %>

但我一直收到:

NoMethodError in Admin::ApplicationController#index
undefined method `page' for ["errors", "messages", "subscribers"]:Array

以下是我的代码:

Application_Controller.rb

class Admin::ApplicationController < InheritedResources::Base
protect_from_forgery
include ResourcesHelper
layout "admin"

#Setup
before_filter :set_resource_variable
before_filter :set_pagination_variable, only: :index

#Authentication
skip_before_filter :authenticate_user!
before_filter :authenticate_admin!

#Authorization
skip_before_filter :check_authentication

#Index
#Custom Index For Application/Index (no inheritance) 
def index
    @users = User.all
    @attributes = %w(messages subscribers)
    @paginate = %w(errors messages subscribers).page(params[:page]).per(15)
end

#Create
def create
    create! { collection_path }
end

#Update
def update
    update! { collection_path }
end


private
#Set Model Variable
def set_resource_variable
    @resource = self.resource_class
    @model = "#{@resource}".downcase.to_sym
end 

#Pagination
def set_pagination_variable
    #params[:page] ||= "1"
end

#Strong Params
def permitted_params
    attributes = attributes(@resource) + %w(user_id admin_id) + [image_pages_attributes: [:caption, image_attributes: [:image]]]
    params.permit(@model => attributes)
end

_table.html.erb

<h2><%= model %></h2> 
<% unless collection.blank? %>
<table class="sort">
    <thead>
        <tr>
            <% model.attribute_names.each do |attr| %>
                <th><%= model.human_attribute_name(attr) %></th>
            <% end %>
            <th colspan="2">&nbsp;</th>
            <% if model.name == "Error" %><th>&nbsp;</th><% end %>
        </tr>
    </thead>
    <tbody>
        <%= render partial: "admin/resources/table/row", collection: collection, as: :resource, locals: {model: model}  %>
    </tbody>
</table>

_row.html.erb

<tr data-link="<%= polymorphic_path([:admin, resource]) %>">
<% model.attribute_names.each do |attr| %>
    <td><%= resource.send(attr) %></td>
<% end %>
<% if model.name == "Error" %>
    <td><%= link_to "Read", admin_error_read_path(resource.id), method: :put, remote: :true, class: "read" %></td>
<% end %>
<td><%= link_to "Edit", edit_polymorphic_path([:admin, resource]) %></td>
<td><%= link_to "Delete", polymorphic_path([:admin, resource]), method: :delete, data: {confirm: "Are you sure?"} %></td>

如果您还需要其他任何东西,请告诉我,我会把它拿出来。

谢谢!

1 个答案:

答案 0 :(得分:0)

您没有正确设置“页面”。应该总是这样:

paginate(:page => params[:page], :per_page => 15)

所以在你的例子中

@paginate = %w(errors messages subscribers).page(params[:page]).per(15)

你真正想要的是:

@paginate = %w(errors messages subscribers).paginate(:page => params[:page], :per_page => 15)