drapergem / draper

Decorators/View-Models for Rails Applications
MIT License
5.22k stars 527 forks source link

Draper::UninferrableDecoratorError (Could not infer a decorator for Document.) #891

Closed denmarkmeralpis closed 1 week ago

denmarkmeralpis commented 3 years ago

How can I infer a namespaced decorator? I got an error(which is expected) everytime I decorate Document model.

2.5.3 :010 > Document.first.decorate.created_at

Draper::UninferrableDecoratorError (Could not infer a decorator for Document.)
# app/decorators/example_namespace/document_decorator.rb
module ExampleNamespace
   class DocumentDecorator < ::Draper::Decorator
      delegate_all

      def created_at
         object.strftime('%D %r')
      end
   end
end

# app/models/document.rb
class Document < ApplicationRecord
...
end
pduersteler commented 3 years ago

Did you had a look at #545 ? Just came across this as well.

Or if it helps, you could specify it in your controller with

decorates_association :document with: ExampleNamespace::DocumentDecorator
pedrohgrandin commented 3 years ago

If you use Spring, try:

spring stop

and restart your server/console

n-rodriguez commented 3 years ago

See: https://github.com/drapergem/draper/pull/899

Alexander-Senko commented 1 week ago

How can I infer a namespaced decorator?

You can define it explicitly in your model:

class Document < ApplicationRecord
  def self.decorator_class(...) = ExampleNamespace::DocumentDecorator
end