This project is archived and is in readonly mode.
ActiveResource validations throws exceptions if errors missing
Reported by Christian Seiler | January 4th, 2010 @ 05:30 PM | in 3.0.2
If the JSON response lacks the 'errors' element, a nil exception is thrown:
undefined method each' for nil:NilClass
/usr/lib/ruby/gems/1.8/gems/activeresource-2.3.5/lib/active_resource/validations.rb:206:in
from_array'
This happens in my case because the resource controller does not render :json => @user.errors. You could say it should, but still I think Rails should be more robust in this case.
Monkeypatch in my project:
module ActiveResource
class Errors
def from_json(json)
array = ActiveSupport::JSON.decode(json)['errors'] rescue []
array ||= [] # THIS IS THE PATCH
from_array array
end
end
end
Same applies to XML (probably).
Comments and changes to this ticket
-
David Trasbo June 20th, 2010 @ 04:13 PM
I've attached a patch that makes sure
from_json
doesn't pass anil
value tofrom_array
.Also, I don't know if a regression test is needed, but in my opinion this more of an edge case than a bug - dealing with JSON that doesn't have an
errors
key is the exception rather than the rule. -
Santiago Pastorino June 20th, 2010 @ 04:51 PM
Can you provide a failing test case. Christian you should follow this https://rails.lighthouseapp.com/projects/8994/sending-patches to get your patches applied ;)
-
José Valim June 22nd, 2010 @ 04:40 PM
- Milestone cleared.
- Assigned user set to José Valim
Yes, please do include tests.
-
David Trasbo June 26th, 2010 @ 06:20 PM
- Importance changed from to Low
Sorry for the delay. Forgot to watch this ticket. I've attached a new patch with a test that fails without the change to
Errors#from_json
and passes with it. Also took the liberty to fix a test method name that seemed a bit illogical. -
Repository June 26th, 2010 @ 07:23 PM
(from [df083b482d3f22aebb5e48e608811a740037d2aa]) Make sure ActiveResource::Errors#from_json doesn't pass nil to #from_array [#3650 state:commited]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/df083b482d3f22aebb5e48e608811a... -
Rohit Arondekar June 27th, 2010 @ 01:38 AM
- State changed from new to committed
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 3650 ActiveResource validations throws exceptions if errors missing (from [df083b482d3f22aebb5e48e608811a740037d2aa]) Make su...