How should I use Draper in my ApplicationController?

怎甘沉沦 提交于 2019-12-12 13:30:12

问题


My questions refers to the following development stack:

  • Rails 3.2.1
  • Draper 0.14
  • Ancestry 1.2.5

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

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?


回答1:


Make your PublicationDecorator available in your ApplicationController.

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


来源:https://stackoverflow.com/questions/10870306/how-should-i-use-draper-in-my-applicationcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!