2014-01-13 74 views
0

我正在嘗試用Kaminari寶石分頁多個表。我試着在我的應用程序控制器,產生一個數組做到這一點,像這樣:與Kaminari寶石發生故障

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

如果你有什麼事,讓我知道,我把它。

謝謝!

回答

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) 
+0

非常感謝我給它一個嘗試:-) –

+0

讓我知道它是怎麼回事......如果有幫助,不要忘記接受答案。 :) –

+0

仍然不怕工作。我有同樣的錯誤接受這一次它不是'未定義的方法頁'它現在'未定義的方法分頁' –