Rails: rendering XML adds tag

前端 未结 2 774
有刺的猬
有刺的猬 2021-02-13 16:02

I\'ve got a Rails controller which is going to output a hash in XML format - for example:

class MyController < ApplicationController
  # GET /example.xml
  de         


        
2条回答
  •  猫巷女王i
    2021-02-13 16:27

    I was having the same Issue;

    This is my XML:

    
    
      
      
    
    

    I was using this:

    entries.to_xml
    

    to convert hash data into XML, but this wraps entries' data into

    So I modified:

    entries.to_xml(root: "Contacts")
    

    but that still wrapped the converted XML in 'Contacts'. modifying my XML code to

    
     
      
       
        
        
        
       
      
     
    
    

    So it adds an extra ROOT that I don't wan't there.

    Now solution to this what worked for me is:

     entries["Contacts"].to_xml(root: "Contacts")
    

    that avoids or any additional root to be included. Cheers!!

提交回复
热议问题