Search code examples
ruby-on-railsdecoratordraper

Decorate relation object in a parent objects view partial


I'm using Draper to decorate my objects.

I have a model 'starts' that has_one horse. I have a horse decorator that has a boy_or_girl method that I have refactored form the horse mode.

#{start.horse.boy_or_girl}"

I'm getting a method not found on the boy_or_girl method. How do i decorate the related horse?


Solution

  • Can't you just call draper in your partial #{start.horse.decorate.boy_or_girl}"?

    If you want to call the decorator only in the controller, you should use decorates_association

    I guess you will have something like

    class StartDecorator < Draper::Base
      decorates :start
      decorates_association :horses
      ...
    end
    
    class HorseDecorator < Draper::Base
      decorates :horse
    
      def boy_or_girl
        # your code
      end
      ...
    end
    

    See also this question