8

I am hitting all kinds of walls trying to stop rails from replacing XML underscores with dashes. I'm doing a post to a web service using ActiveResource. I have tried all kinds of variations of fixes for this, with results varying from rails initialization errors to just no effect. The web service I am posting to requires underscores.

Essentially, if I can get the following in place, I should be good:

From http://rubydoc.info/docs/rails/2.3.8/ActiveResource/Base

:dasherize - Boolean option to determine whether or not element names should replace underscores with dashes. Default is true. The default can be set to false by setting the module attribute ActiveSupport.dasherize_xml = false in an initializer.

Can someone provide an example of this? I'm unfortunately on a tight timeline, so if someone can provide assistance that would be a huge help.

Thanks!

slimchrisp
  • 101
  • 1
  • 3

1 Answers1

15

Did you attempt to set the module attribute to false in an initializer?

ActiveSupport.dasherize_xml = false

for rails 3+ you can use the following within your response:

render :xml => object.to_xml(:dasherize => false)

See edit history for ugly monkeypatching approach.

20man
  • 1,419
  • 13
  • 11
  • I attempted, but had various errors including ActiveSupport not having that property. If someone can give me a proper example of an initializer setting that property that would get me what I need. Right now I am just overriding to_xml in each ActiveResource model – slimchrisp Mar 25 '11 at 23:17
  • 1
    check the updated answer to see if it is of any use. You can also extend your own subclass of ActiveResource to make this even cleaner. – 20man Mar 26 '11 at 05:24
  • +1. two comments: 1) AFAIK `render :xml => object.to_xml(:dasherize => false)` should work in Rails 2. 2) Why show hacking solution (last 2 snippets) when the other two ways you show work? Don't encourage unnecessary monkeypatching. – tokland Jan 16 '13 at 11:28
  • 1
    @tokland the edit history shows just what you suggest; I agree, monkeypatching is nearly always a sin. In the spirit of a more current answer, I'm removing the ugly. Thanks! – 20man Feb 01 '13 at 20:16