我应该如何在ApplicationController中使用Draper?

我的问题涉及以下开发堆栈:

  • Rails 3.2.1
  • 德雷珀0.14
  • 祖先1.2.5

我想要做的是,将导航提供给我的布局。 所以我在ApplicationController定义了一个beforefilter。

 class ApplicationController < ActionController::Base [..] before_filter :current_navigation [..] def current_navigation @n = PublicationDecorator.find(1) end end 

正如您在上面的代码清单中看到的那样,我正在使用draper 。 我的PublicationDecoratorApplicationController不可用。 那么如何让我的所有Publications装饰好?

 uninitialized constant ApplicationController::PublicationDecorator 

我正在使用ancestry gem来实现层次结构。 还有一个问题是,如果我使用的是ancestry ,那么所有物品都会被装饰吗?

ApplicationController中使您的PublicationDecorator可用。

 require 'publication_decorator.rb' # <-- class ApplicationController < ActionController::Base [..] before_filter :current_navigation [..] def current_navigation @n = PublicationDecorator.find(1) end end 

为了让孩子甚至父母装饰添加到你的装饰者的关联:

 class PublicationDecorator < Draper::Base decorates :publication decorates_association :children [..] end