0

I'd like to simple_format the body of my CommentSerializer. I have:

class CommentSerializer < ActiveModel::Serializer
  attributes :id, :body 

  def body
    simple_format(body)
  end

but this goes into a recursive call. Ideally, I'd like to keep the body attribute as there is already front-end code using it. What would be an easy way to add this?

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
timpone
  • 19,235
  • 36
  • 121
  • 211

1 Answers1

1

It goes in a loop because you call body from body. If you want to format the original model body, you need to use object.body.

def body
  simple_format(object.body)
end

Note that object is the reference to the object you pass when you initialize the serializer instance.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364