【问题标题】:Draper with Bootstrap Pagination - undefined method 'total_pages'带有引导分页的 Draper - 未定义的方法“total_pages”
【发布时间】:2015-05-07 09:14:31
【问题描述】:

我正在使用 Draper 来装饰我的视图并从中移除一些逻辑,但我正在努力解决这个问题 - 如何使用 Bootstrap Pagination (will_paginate) 设置 Draper?

默认情况下我有这个:

delegate_all

从 Draper 文档中,我尝试添加以下内容:

delegate :current_page, :per_page, :offset, :total_entries, :total_pages

但是在视图中调用分页时仍然会返回错误。 我的控制器这样定义装饰和分页:

@matches = Match.all.paginate(:per_page => 10, :page => params[:page]).decorate

我的看法:

<%= will_paginate @matches, renderer: BootstrapPagination::Rails %>

更新:

class ApplicationDecorator < Draper::Decorator
  def self.collection_decorator_class
    PaginatingDecorator
  end
end


class PaginatingDecorator < Draper::Decorator
  # support for will_paginate
  delegate :current_page, :total_entries, :total_pages, :per_page, :offset
end


class PlayerDecorator < ApplicationDecorator
  delegate_all
  decorates_association :matches


class MatchDecorator < ApplicationDecorator
  delegate_all
  decorates_association :players

【问题讨论】:

    标签: ruby-on-rails twitter-bootstrap decorator will-paginate draper


    【解决方案1】:

    https://github.com/drapergem/draper/issues/429

    # app/decorators/application_decorator.rb
    class ApplicationDecorator < Draper::Decorator
      def self.collection_decorator_class
        PaginatingDecorator
      end
    end
    
    # app/decorators/paginating_decorator.rb
    class PaginatingDecorator < Draper::CollectionDecorator
      # support for will_paginate
      delegate :current_page, :total_entries, :total_pages, :per_page, :offset
    end
    
    # app/decorators/whatever_decorator.rb
    class WhateverDecorator < ApplicationDecorator
    end
    

    【讨论】:

    • 我实际上是在使用all 按日期订购商品。整行是:` @matches = Match.all.order( "date DESC" ).paginate(:per_page => 10, :page => params[:page]).decorate ` 但我确实尝试过不使用all它并没有改变任何事情。仍然是一个错误。
    • 这是否意味着我必须创建一个PaginationDecorator 并为其添加委托?
    • 是的,这样您就可以在其他模型上重用该 PaginationDecorator 进行分页。
    • 好的,添加完之后我遇到了这个问题:issue#650
    • 请用 Draper::CollectionDecorator 类 PaginatingDecorator 继承分页装饰器
    猜你喜欢
    • 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
    相关资源
    最近更新 更多