generating XML in rails
- by PeterWong
I created a controller having an action:
def gen_books_xml
@books = Book.find(:all, :conditions => {:owner_id => 1})
respond_to do |format|
format.xml { render :xml => @books.to_xml(:root => "Books", :skip_types=>true) }
end
end
How could I implement the to_xml method in the Book model sa that it can generate the following format?
<?xml version="1.0" encoding="UTF-8"?>
<Books>
<Owner>1</Owner>
<Book><title>some title</title></Book>
<Book><title>some title</title></Book>
<Book><title>some title</title></Book>
...
</Books>
where there is only 1 Owner element and many Book elements
I can only output the Book elements but cannot output the one Owner in the same level of Books. Please HELP!!!