2012-06-03 14 views
3

我的問題是指下列開發堆棧:我應該如何在我的ApplicationController中使用Draper?

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

我想要做的是,提供的導航我的佈局。所以我在我的ApplicationController中定義了一個過濾器。

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

正如你在代碼中看到上面的列表中,我使用的draper。我的PublicationDecorator不適用於ApplicationController那麼我如何獲得我所有的Publications裝飾?

uninitialized constant ApplicationController::PublicationDecorator 

我正在使用ancestry gem來實現層次結構。 A 進一步的問題是,將所有的對象裝飾,如果我使用ancestry

+0

類似http://stackoverflow.com/questions/10884740/nested-attributes-with-draper-and -rails-3 – Robin

回答

3

在您的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 
+0

謝謝你的回答!我的問題更多的是,我是否理解在這種情況下使用給定的技術。請注意我的「之間」和「進一步」問題。 – Robin

+0

增加了一些信息給你的答案,它完全回答我的問題。 – Robin

+0

***來吧*** –