[英]Having Trouble With Kaminari Gem
我正在嘗試使用Kaminari寶石對多個桌子進行分頁。 我試圖通過在應用程序控制器中生成一個數組來做到這一點,如下所示:
@paginate = %w(errors messages subscribers).page(params[:page]).per(15)
然后,我采用@paginate
並將其用於我的部分@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"> </th>
<% if model.name == "Error" %><th> </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>
如果您還有其他需要,請告訴我,我會解決。
謝謝!!
您沒有正確設置“頁面”。 應該總是這樣:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.