ruby-on-rails – Rails:渲染XML添加标签
发布时间:2020-12-16 19:26:29 所属栏目:百科 来源:网络整理
导读:我有一个Rails控制器,它将以 XML格式输出哈希,例如: class MyController ApplicationController # GET /example.xml def index @output = {"a" = "b"} respond_to do |format| format.xml {render :xml = @output} end endend 但是,Rails添加了 hash标签,我
我有一个Rails控制器,它将以
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>标签,我不想要,即: <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> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |