좋아, ActiveRecord 객체의 단일 인스턴스에 대해 to_xml 메서드를 재정의 할 수 있다는 것을 알고 있으며 나에게 잘 작동합니다. 하지만 개체 컬렉션에 대해 to_xml 메서드를 재정의하려면 어떻게해야합니까?ActiveRecord 객체를 수집 할 때 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'
를 얻을?
render :xml => @tasks
은 자동으로되지 방법을 렌더링하여 제작 된 to_xml을 선택 하는가 : 당신은 그냥 사용하는 경우
아아를 선언해야한다. 나는 그것이 작동하지 않는다는 것을 안다 :) 이것은 해결책이다. 개체 컬렉션을 직렬화 할 개체로 처리해야합니다. 'render : xml => @ tasks'와 같이 보이고 그것을 당신의 솔루션과 비교한다면 알게 될 것입니다. – Chirantan