nesquena / rabl

General ruby templating with json, bson, xml, plist and msgpack support
http://blog.codepath.com/2011/06/27/building-a-platform-api-on-rails/
MIT License
3.64k stars 334 forks source link

Migrating to Rails 5 - Rabl returning Active Record object #715

Open frank184 opened 6 years ago

frank184 commented 6 years ago

We are migrating to Ruby on Rails 5, and upgrading to rabl >= 0.13.

Everything looks good except this one request that is returning a stringyfied object instead of a view.

JSON::ParserError:
       784: unexpected token at '#<ReferenceNumber::ActiveRecord_Relation:0x00007fda690fedd0>'

We are using grape and grape-rabl and the request looks like so:

desc 'Deletes a reference ID'
params do
  requires :device_id, type: String, desc: 'Device Identifier.'
  requires :cardnumber, type: String, desc: 'Card number.'
  requires :reference_number, type: String, desc: 'Reference ID'
end
delete '/reference_numbers', rabl: 'devices/cardholder/reference_numbers', http_codes: [
  [...business errors...]
  [401, 'Unauthorized'],
  [403, 'Forbidden - trying to access resource that is not authorized for this merchant']
] do
    ... business logic ...
    @reference_numbers = ReferenceNumber.by_card_number(params[:cardnumber])
        .by_participating_businesses(@device.participating_business)
end

The rabl view file looks like this:

node(:reference_numbers) do
  @reference_numbers.map do |reference_number|
    {
      number: reference_number.number,
      business: reference_number.participating_business.name
    }
  end
end

Where @reference_numbers is equal to an empty ActiveRecord_Relation, the view returns that object.

Whenever @reference_numbers has results then everything works properly.

Might not be understanding the changes between Rabl 0.12 and 0.13, if anyone understands whats going on or has any suggestions, please let me know!

EDIT

I've tried testing very similar requests and to no avail cannot reproduce this issue.

Then I tried commenting out the @reference_numbers = line and the rabl file is rendered.

Strange since if it were that line, then you'd expect the same kind of issue throughout where this line appears, but this is not the case.