gpt4 book ai didi

ruby-on-rails - 覆盖 to_xml 以收集 ActiveRecord 对象

转载 作者:数据小太阳 更新时间:2023-10-29 02:32:43 26 4
gpt4 key购买 nike

好的,我知道您可以为 ActiveRecord 对象的单个实例覆盖 to_xml 方法,它对我来说工作得很好。但是我将如何着手覆盖对象集合的 to_xml 方法?

假设对于任务模型实例,我实现了如下所示的 to_xml。

def to_xml
super(:methods => [:tag_list], :include => {:project => {:include => {:folder => {}}}, :folder => {}})
end

当单个任务被序列化为 xml 时工作得很好。但是当我的代码运行收集任务时,如下面的代码

render :xml => @tasks.to_xml

我明白了

wrong number of arguments (1 for 0)

/home/chirantan/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/array/conversions.rb:189:in `to_xml'
/home/chirantan/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/array/conversions.rb:189:in `to_xml'
/home/chirantan/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/array/conversions.rb:189:in `each'
/home/chirantan/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/array/conversions.rb:189:in `to_xml'
/var/lib/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `call'
/var/lib/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `_nested_structures'
/var/lib/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:58:in `method_missing'
/var/lib/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:31:in `tag!'
/~/blah/app/controllers/tasks_controller.rb:412:in `completed'

我如何使它工作?

最佳答案

应该声明您的 to_xml 覆盖

def to_xml options={}
....
end

关于ruby-on-rails - 覆盖 to_xml 以收集 ActiveRecord 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2698498/

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