Rails Rails 3 Rendering XML

Fred · 2012年08月24日 · 最后由 knwang 回复于 2012年08月24日 · 4090 次阅读

HI,大家好 之前曾经讨论过 rails 应用发布 webservice (http://ruby-china.org/topics/4671) 当时最终选择了 rails restful 的方式,感觉确实很好。 近期觉得除 json 格式外,还需要提供 xml 格式的交互。 在尝试的过程中遇到些问题,部分代码如下: respond_to do |format| format.json { render :json => changes_res, :layout => false } format.xml { render :xml => changes_res } end json 方式访问正常,xml 方式访问,有以下错误信息 undefined method `each' for #SpotChangesResponse:0x00000105d40660

我在想 rails 处理 xml 是不是需要额外的插件,或者对 changes_res 对象本身有什么特殊的需求?

http://guides.rubyonrails.org/layouts_and_rendering.html提到 2.2.9 Rendering XML Rails also has built-in support for converting objects to XML and rendering that XML back to the caller:

render :xml => @product You don’t need to call to_xml on the object that you want to render. If you use the :xml option, render will automatically call to_xml for you. 我试了,还是不可以。。。

最新进展:在 Controller 的顶部,respond_to :xml, :json action 结尾处:respond_with changes_res json 正常,xml 依旧不可以,错误信息:Missing template remote_spot_details/show with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml, :prawn, :rxls], :formats=>[:xml], :locale=>[:zh, :zh]} in view 后来发现,如果 respond_with 是继承 ActiveRecord 就可以。但是我的 response 对象是与其它公司的系统交互的,不能直接 ActiveRecord 扔给他们,在 ActiveRecord 中,也不知道哪些是处理 xml 相关的,大家有什么好的方法没?

需要 登录 后方可回复, 如果你还没有账号请 注册新账号