Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

This project is archived and is in readonly mode.

ActiveResource validations throws exceptions if errors missing

#3650

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:infrom_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).

Reported by Christian Seiler · January 4th, 2010 @ 05:30 PM

State: committed
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. David Trasbo
    David Trasbo

    I've attached a patch that makes sure from_json doesn't pass a nil value to from_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.

    June 20th, 2010 @ 04:13 PM

  2. Santiago Pastorino
    Santiago Pastorino

    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 ;)

    June 20th, 2010 @ 04:51 PM

  3. José Valim
    José Valim
    • Milestone cleared.
    • Assigned user set to José Valim

    Yes, please do include tests.

    June 22nd, 2010 @ 04:40 PM

  4. David Trasbo
    David Trasbo
    • 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.

    June 26th, 2010 @ 06:20 PM

  5. Repository
  6. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to committed

    June 27th, 2010 @ 01:38 AM

  7. Jeremy Kemper