standup75
standup75

Reputation: 4814

to_xml giving strange results

When I do that

{"New York"=>33, :Versailles => 3231}.to_xml

I get

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<hash>
  <Versailles type=\"integer\">3231</Versailles>
  <New York type=\"integer\">33</New York>
</hash>

I would have expected rails to dasherize "New York", no?

Upvotes: 5

Views: 676

Answers (3)

Prakash Murthy
Prakash Murthy

Reputation: 13077

This issue is closed via the merging of pull request 445 : https://github.com/rails/rails/pull/445

Spaces will now be dasherized (and the private _dasherize method is enhanced to handle spaces.)

{"New York"=>33}.to_xml will result in

..<New-York type=\"integer\">33</New-York>..

Thanks for putting up this lighthouse ticket & the stackoverflow question (which added more info in the discussions); with the help of all the information provided, I was able to make my first rails commit!

Upvotes: 2

adamlamar
adamlamar

Reputation: 4927

Normally database fields don't have spaces in them, so your example in the context of #to_xml is a garbage-in-garbage-out situation.

Upvotes: 0

the Tin Man
the Tin Man

Reputation: 160571

I'm seeing the same thing too.

According to the docs the :dasherize option to to_xml should do the trick.

Some configuration is available through options. [...] This behavior can be controlled with :only, :except, :skip_instruct, :skip_types, :dasherize and :camelize [...] The default is to dasherize all column names, but you can disable this setting :dasherize to false. Setting :camelize to true will camelize all column names - this also overrides :dasherize.

So, it looks like, at a minimum:

asdf.to_xml(:dasherize => true)

should do it, and adding the :camelize option should force it.

=> "<?xml version="1.0" encoding="UTF-8"?>\n<Hash>\n  <New York type="integer">33</New York>\n  <Versailles type="integer">3231</Versailles>\n</Hash>\n"

So, maybe someone can 'splain the anomoly.

Upvotes: 0

Related Questions