问题描述
我的问题涉及以下开发堆栈:
My questions refers to the following development stack:
- Rails 3.2.1
- Draper 0.14
- 祖先1.2.5
我要做的是,将导航传递到我的布局。因此,我在 ApplicationController
中定义了一个前置过滤器。
What I want to do is, deliver the navigation to my layout. So I've defined a before filter in my ApplicationController
.
class ApplicationController < ActionController::Base
[..]
before_filter :current_navigation
[..]
def current_navigation
@n = PublicationDecorator.find(1)
end
end
如上面的代码清单所示,我使用的是斗篷
。我的 PublicationDecorator
在 ApplicationController
中不可用。 所以如何装饰所有出版物
?
As you see in the code listing above, I'm using the draper
. My PublicationDecorator
isn't available in the ApplicationController
. So how do I get all my Publications
decorated?
uninitialized constant ApplicationController::PublicationDecorator
我正在使用祖先
来实现层次结构。一个进一步的问题是,如果我使用的是祖先
?
I'm using the ancestry
gem to realize a hierarchy. A further question is, will be all objects decorated, if I'm using ancestry
?
推荐答案
在您的 ApplicationController
中使您的 PublicationDecorator
可用。
require 'publication_decorator.rb' # <--
class ApplicationController < ActionController::Base
[..]
before_filter :current_navigation
[..]
def current_navigation
@n = PublicationDecorator.find(1)
end
end
要装饰孩子或父母,请将关联添加到装饰器中:
To get children or even parents decorated add the association to your decorator:
class PublicationDecorator < Draper::Base
decorates :publication
decorates_association :children
[..]
end
这篇关于我应该如何在ApplicationController中使用Draper?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!