Hi all I’m using active_resource and have next question.
sometimes there is destroy validation needed
Example
def destroy
if @user.destroy
head :ok
else
respond_with(@user)
end
end
user model can’t be deleted in few cases. If it wasn’t delete @user contains errors after validation before_delete
and responder serialize it like that
=> {\"errors\":{\"id\":[\"Cannot delete user with orders\"]}}
but active_resource don’t handle such response and fails with
ActiveResource::ResourceInvalid: Failed. Response code = 422. Response message = .
what is the best way to handle such situations?
I have never used ActiveResource so far, however I think this is the right way to implement a destroy method… or at least it’s the REST way.
You said ActiveResource doesn’t handle such response, but actually that’s exactly the opposite. The ActiveResource::ResourceInvalid error is raised because of the 422 (Unprocessable Entity) response.
More infos here : API ActiveResource
Here is what the Resource errors part says :
To sum up, these errors allow you to handle and behave accurately depending on the status code returned. For instance you don’t act the same way whether the response is a 503 (Service Unavailable) or a 422 (Unprocessable Entity). In first case you will probably said “Try again later” and in the second one “Cannot do that because of [message returned]”.