4

我正在尝试使用 Draper 装饰由 public_activity gem 创建的活动集合。

但是,似乎 PublicActivity::Activity 没有从 ActiveRecord 继承(我是否正确地假设?)所以我需要添加include Draper::Decoratable到模型中,但是那个 PublicActivity 模型到底在哪里?

顺便说一句,这是我得到的错误:

Could not infer a decorator for PublicActivity::Activity.

4

2 回答 2

3

好吧,事实证明这是行不通的:

class StudentActivitiesController < ApplicationController
  def index
    @activities = PublicActivity::Activity.order("created_at desc").decorate
  end
end

我猜这是因为命名空间查询PublicActivity::Activty不是从 ActiveRecord 继承的,因此不包括Draper::Decoratable,但我不确定。如果我错了,请纠正我。

但是,使用 Draper,您可以装饰对象以及查询,所以这确实有效......

class StudentActivitiesController < ApplicationController
  def index
    @activities = PublicActivity::Activity.order("created_at desc")
    @activities = StudentActivitiesDecorator.decorate_collection(@activities)
  end
end

装饰器还必须包括decorates PublicActivity::Activity

于 2013-12-27T16:39:40.893 回答
3

只需activity_decorator.rb输入decorators/public_activity

# app/decorators/public_activity/activity_decorator.rb

class PublicActivity::ActivityDecorator < Draper::Decorator
end
于 2014-02-15T04:16:40.297 回答