gpt4 book ai didi

ruby-on-rails - rails : rendering XML adds tag

转载 作者:行者123 更新时间:2023-12-03 23:23:47 25 4
gpt4 key购买 nike

我有一个 Rails Controller ,它将以 XML 格式输出哈希 - 例如:

class MyController < ApplicationController
# GET /example.xml
def index
@output = {"a" => "b"}

respond_to do |format|
format.xml {render :xml => @output}
end
end
end

但是,Rails 添加了一个我不想要的 标签,即:
<hash>
<a>
b
</a>
</hash>

我怎样才能输出这个呢?
<a>
b
</a>

最佳答案

我认为如果要将对象转换为 XML,则需要一个包含所有内容的标签,但您可以自定义包装器的标签名称:

def index        
@output = {"a" => "b"}

respond_to do |format|
format.xml {render :xml => @output.to_xml(:root => 'output')}
end
end

这将导致:
<output>
<a>
b
</a>
</output>

关于ruby-on-rails - rails : rendering XML adds <hash> tag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6241911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com