我正在尝试使用Draper修饰由public_activity gem创建的一系列活动。
但是,看起来PublicActivity :: Activity不会从ActiveRecord继承(我是否正确地假设?)所以我需要将include Draper::Decoratable
添加到模型中,但PublicActivity模型究竟在哪里?< / p>
是的,这是我得到的错误:
Could not infer a decorator for PublicActivity::Activity.
答案 0 :(得分: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
答案 1 :(得分:3)
只需将activity_decorator.rb
放入decorators/public_activity
:
# app/decorators/public_activity/activity_decorator.rb
class PublicActivity::ActivityDecorator < Draper::Decorator
end