Rails:渲染 XML 添加 <hash>标签 [英] Rails: rendering XML adds <hash> tag
本文介绍了Rails:渲染 XML 添加 <hash>标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 Rails 控制器,它将以 XML 格式输出哈希 - 例如:
class MyController <应用控制器# 获取/example.xml定义索引@output = {"a" =>"b"}response_to do |格式|format.xml {render :xml =>@输出}结尾结尾结尾
然而,Rails 添加了一个 <hash>标签,我不想要,即:
<a>乙</a></hash>
我怎样才能只输出这个?
乙</a>
解决方案
我认为,如果您要将对象转换为 XML,您需要一个包装所有内容的标签,但您可以为包装器自定义标签名称:
>
def 索引@output = {"a" =>"b"}response_to do |格式|format.xml {render :xml =>@output.to_xml(:root => '输出')}结尾结尾
这将导致:
I've got a Rails controller which is going to output a hash in XML format - for example:
class MyController < ApplicationController
# GET /example.xml
def index
@output = {"a" => "b"}
respond_to do |format|
format.xml {render :xml => @output}
end
end
end
However, Rails adds a <hash> tag, which I don't want, i.e.:
<hash>
<a>
b
</a>
</hash>
How can I just output this instead?
<a>
b
</a>
解决方案
I think if you're converting an object to XML, you need a tag which wraps everything, but you can customise the tag name for the wrapper:
def index
@output = {"a" => "b"}
respond_to do |format|
format.xml {render :xml => @output.to_xml(:root => 'output')}
end
end
Which will result in:
<output>
<a>
b
</a>
</output>
这篇关于Rails:渲染 XML 添加 <hash>标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文