Open ghost opened 7 years ago
What's the stack for that error? I think you're overwriting an @options
that is set somewhere in Grape. The endpoints should be isolated and anything you do inside of them shouldn't affect anything else obviously, so lets call it a bug. It would be helpful if you could write a test for it.
To fix your issue, don't set @options
, but a local var options
. The only reason to set instance variables like @options
is to carry these across functions, which is impossible with a singleton approach like a Grape API anyway.
@dblock thank you! We'll remove all instance variables from the Grape controllers then! I am at work now, but I'd be happy to write a test for it later this weekend. Yes.. looks like a 'leaked' variable.
@dblock mmm... it appears that I need to keep the @
as my .rabl
views are not able to access any local variable from some other method (as I should have expected)
You should be passing the data into the rabl formatter in some other way.
Hello, I have been using Grape to build an API within an already-existing Rails project. I have been dealing with an error that I do not understand and I wonder if somebody could draw some light on this as it could be a bug but I am not sure.
My colleague and I were tasked with creating 4 end-points for this API:
/packages
/packages/:id
/options
/options/:id
This is what my colleague wrote (which works perfectly!):
And this is what I wrote (basically the exact same, but for
options
):The first end-point (
/options/:id
) works but the second one throws an error.This is because basically
@options
is expected to be aHash
and not anArray
(or ActiveRecord::Relation). Further debugging made me discover that using a different variable (something other than@options
) fixed the issue!But why is this?
In the first example (my colleague's code),
@packages
is set tonil
before being set to whatever we set it to. But in my case@options
was assigned to:and (re-)assigning it to an array, a collection or an ActiveRecord::Relation broke the code. For now, I'll just re-name my variable to something else but I'd love to hear somebody else's opinion on this as I don't think this behaviour is expected.