7

I have a decorator that returns either a string, or a collection.

In my view, I want to display the return of this decorator method.

class MyDecorator < Draper::Decorator
  def stuff
    if condition
      'some string'
    else
      render model.some_collection
    end
  end
end

Now in my view I want to render the string if it's a string, or render the collection.

= my_object.stuff

Unfortunately I cannot access the render method inside Draper::Decorator. Thoughts?

ardavis
  • 9,842
  • 12
  • 58
  • 112

2 Answers2

8

From https://github.com/drapergem/draper#accessing-helpers:

Normal Rails helpers are still useful for lots of tasks. Both Rails' provided helper and those defined in your app can be accessed via the h method

So, replace render with h.render and it should work fine.

Justin Thomas
  • 283
  • 1
  • 6
  • I haven't had an opportunity to implement that fix yet, thank you for your answer. I will mark it as complete once I verify that it works. I can't believe I missed that in the Readme... – ardavis Jun 04 '13 at 15:05
2

you can include Draper's Draper::LazyHelpers module inside the decorator to use the ActionView::Helpers and the rest of ActionView as you've always have with views.

Kevin_L22
  • 113
  • 7