简体   繁体   English

Kaminari宝石遇到麻烦

[英]Having Trouble With Kaminari Gem

I'm trying to paginate multiple tables with the Kaminari Gem. 我正在尝试使用Kaminari宝石对多个桌子进行分页。 I've tried to do this by generating an array in my application controller, like so: 我试图通过在应用程序控制器中生成一个数组来做到这一点,如下所示:

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

I've then taken @paginate and used it in one of my partials than renders the tables, like so: 然后,我采用@paginate并将其用于我的部分@paginate而不是呈现表,如下所示:

<%= paginate @paginate %>

But I keep receiving a: 但我一直收到:

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

Here is the code that I've got: 这是我得到的代码:

Application_Controller.rb 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

end 结束

_table.html.erb _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 _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>

If you need anything else, let me know and I'll put it up. 如果您还有其他需要,请告诉我,我会解决。

Thank you!! 谢谢!!

You're not setting "page" properly. 您没有正确设置“页面”。 It should always be like this: 应该总是这样:

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

So in your example 所以在你的例子中

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

What you really want is: 您真正想要的是:

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

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

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