好的,我知道你可以覆盖单个ActiveRecord对象实例的to_xml方法,它对我来说效果很好。但是,如何重写to_xml方法来收集对象?
假设对于Task模型实例,我实现了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'
我如何使这项工作?
答案 0 :(得分:3)
应声明你的to_xml覆盖
def to_xml options={}
....
end
答案 1 :(得分:0)
如果您只使用以下内容会发生什么:
render :xml => @tasks
渲染方法是否不会自动获取精心制作的to_xml?