how to access a decorator Admin::ExampleDecorator in controller?

915 views Asked by At

I have used draper gem to generate the decorator and I have crated a decorators. I have used namespacing inside decorator folder, and I tried to access the that decorator in specific controller with decorates_assigned.

For example my folder structure is decorators/admin/example_decorator.rb

And I have added the following line in controller:

decorates_assigned :example 

and in action I have written

def edit
  @example = Admin::ExampleDecorator.new(Example.last)
end

But I am getting following error:

Could not infer a source for Admin::ExampleDecorator.

I am not understanding why this error is coming.

How namespaces are works in Decorator?

1

There are 1 answers

0
rpbaptist On

I am not entirely sure about your question, but since you are using decorated_assigned :example you are creating a helper method. See the Draper readme.

You won't need to call the decorator explicitely.

def edit
  @example = Example.last
end

This means in your view you will call example instead of @example to get the decorated version. This will call your helper method and returns the decorated version.