trailblazer / roar-rails

Use Roar's representers in Rails.
http://roar.apotomo.de
MIT License
235 stars 65 forks source link

representer returns blank string #137

Open hammer65 opened 5 years ago

hammer65 commented 5 years ago

I have a controller which is working fine for every method but an update method which needs to return the edited contents of a record so that the updated datetime and which user edited the record can be used by the requesting application. Instead of returning this data however it returns a blank string.

If I manually render some JSON it works fine. In this same controller I have a create method and a show method which both use the representer and that returns data as expected.

def update   
    if credential.save
      consume! credential, represent_with: App::Configuration::CredentialRepresenter
      respond_with credential, represent_with: App::Configuration::CredentialRepresenter
    else
      render json: { error: credential.errors.full_messages.to_sentence }, status: :unprocessable_entity
    end
  end

if instead I send data this way

if credential.save
      render json: {
        modifiedBy: credential.modified_by,
        updatedAt: credential.updated_at
      }
else

The data comes through just fine as it is in the code above with a standard JSON render. Using the representer results in a blank string being sent. The only real difference between this update method and the create and show methods where the representer works as expected is that consume! is called before the save method, which should not make a difference. Obviously I can't derive the updated datetime until it's saved and the values I need are present in the model because I can send them with a standard json render.

Under what conditions would a representer render a blank string like this?