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.

This project is archived and is in readonly mode.

ActiveResource errors.from_xml Can't handle symboled attribute keys because of humanize call

#1472

If you look at:


    # Grabs errors from the XML response.
    def from_xml(xml)
      clear
      humanized_attributes = @base.attributes.keys.inject({}) { |h, attr_name| h.update(attr_name.humanize => attr_name) }
      messages = Hash.from_xml(xml)['errors']['error'] rescue []
      messages.each do |message|
        attr_message = humanized_attributes.keys.detect do |attr_name|
          if message[0, attr_name.size + 1] == "#{attr_name} "
            add humanized_attributes[attr_name], message[(attr_name.size + 1)..-1]
          end
        end

        add_to_base message if attr_message.nil?
      end
    end
  end

You'll notice humanized_attributes = @base.attributes.keys.inject({}) { |h, attr_name| h.update(attr_name.humanize => attr_name) } ... and in particular, attr_name.humanize. If the attribute key is a symbol instead of a string (say you're working directly from the command line), you'll get an exception from that line:


/activeresource-2.2.2/lib/active_resource/validations.rb:205:in `from_xml': undefined method `humanize' for :[whatever attr_name is]:Symbol (NoMethodError)

It seems like this would be a simple fix (just attr_name.to_s.humanize). Is anyone else having this problem?

Reported by Matthew Moore · November 25th, 2008 @ 08:09 PM

State: stale
Milestone: 3.x
Assigned to: Michael Koziarski Michael Koziarski
Importance: none

Activity

  1. Michael Koziarski
    Michael Koziarski

    This sounds like a good fix to me, attach a patch with tests and we're good to go.

    December 1st, 2008 @ 08:01 PM

  2. Matthew Moore
    Matthew Moore

    Actually I've been looking into this more, and it might be better instead to stringify_keys! in the attributes= method like ActiveRecord does. Still thinking about it...

    Do you have any thoughts on what you'd prefer?

    December 1st, 2008 @ 08:13 PM

  3. Michael Koziarski
    Michael Koziarski

    consistency with AR is probably worth doing, plus it makes it a little less ad-hoc compared to sticking .to_s somewhere. It'll also stop any other issues from popping up later.

    December 1st, 2008 @ 08:15 PM

  4. Pratik
    Pratik
    • Title changed from ActiveResource 2.2.2 errors.from_xml Can't handle symboled attribute keys because of humanize call to ActiveResource errors.from_xml Can't handle symboled attribute keys because of humanize call

    March 13th, 2009 @ 11:14 AM

  5. Yuri
    Yuri

    I have just posted a bug report where the cause of the problem is this very method, though the problem is different - #2229

    March 13th, 2009 @ 03:40 PM

  6. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from activeresource, bug to activeresource, bug, bugmash

    August 5th, 2009 @ 03:34 PM

  7. Dan Pickett
    Dan Pickett

    patch attached validated +1

    I took a swing at a patch for this to stringify the keys. pretty simple.

    August 8th, 2009 @ 09:30 PM

  8. Steve St. Martin
    Steve St. Martin

    +1 patch applies cleanly to both 2-3-stable and master, appears to address issue and test is sufficient

    August 8th, 2009 @ 10:12 PM

  9. Rizwan Reza
    Rizwan Reza

    verified

    +1 This patch applies to both: 2-3-stable and master cleanly.

    August 8th, 2009 @ 10:17 PM

  10. Nick Quaranto
    Nick Quaranto

    +1, applies cleanly and passes on both 2-3-stable and master.

    August 8th, 2009 @ 10:17 PM

  11. Dan Croak
    Dan Croak

    +1 verified in 2-3-stable.

    Patch applies cleanly. activeresource rake test runs green.

    August 8th, 2009 @ 10:18 PM

  12. Greg Sterndale
    Greg Sterndale

    +1 verified

    patch applied, tests pass in 2-3-stable

    August 8th, 2009 @ 10:54 PM

  13. Dan Croak
    Dan Croak

    Pratik mentioned in IRC that he'd like to see a test for from_xml added to this patch before it is committed. I started to look into it but am on another computer. If someone gets to it first, have at it!

    August 10th, 2009 @ 02:41 AM

  14. Blue Box Stephen
    Blue Box Stephen

    +1, patch applies cleanly and passes.

    However, in 2.3-stable it looks like from_xml has been changed significantly in 2.3-stable (original code above is from 2.2.2). For example, humanize is no longer called. As such, are you sure we still need this particular test for from_xml? (See lib/active_resource/validations.rb starting line 224 in 2.3-stable).

    September 27th, 2009 @ 01:51 AM

  15. sr.iniv.t
    sr.iniv.t

    +1 verified.

    The patch applies cleanly on 2-3-stable and tests pass. However, a different patch is needed for master because the activeresource test directory structure has changed in master.

    September 27th, 2009 @ 07:34 PM

  16. Rizwan Reza
    Rizwan Reza
    • Tag changed from activeresource, bug, bugmash to activeresource, bug

    February 12th, 2010 @ 12:46 PM

  17. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  18. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open
    • Importance changed from to

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 05:04 PM

  19. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 05:04 PM