Raymond Brigleb
2005-Aug-22 00:03 UTC
XML Builder question (Brain Teaser? or At A Total Loss?)
I''m trying my best to get Builder to give me a nested representation of my nested database tables, but I think I''m not getting any closer to a solution. In fact, now I''m in Syntax Error land, rapidly approaching Missed Deadline country. Perhaps you could be the one to save my ass? Basically, I have Images which have many Options (intended to represent different shirt sizes, for example, and their corresponding prices). I''ve written custom SQL to pull out just the images that belong to a particular website, as you can see in my controller: def website_images_with_options @images = Image.find_by_sql(["select images.* from images join categories on images.category_id = categories.id join websites on categories.website_id = websites.id where websites.id = ? order by images.position", @params[:id]]) @options = Option.find_all render_without_layout end The line beginning with @options is just wrong. I''m attempting to correspond the options to the images, but right now it''s giving me ALL options for each image. But that''s not even the worst part. My controller is rendering an .rxml template of the same name: xml.images do @images.each do |image| xml.image( xml.options { xml.image.option("id" => option.id, "title" => option.title, "price" => option.price) } "id" => image.id, "title" => image.title, "description" => image.description, "category" => image.category.id, "filename" => (image.filename+".jpg") ) end end The problem here is that I''m not sure how Builder both nests XML tags and applies attributes to the tags. I can''t seem to find a good example of this anywhere. Can someone please point me in the right direction on either of my apparent problems here, either figuring out the correct syntax to nest Builder XML with attributes, or how to pass nested database results to Builder? I would be indescribably grateful. Raymond
Jim Weirich
2005-Aug-22 01:58 UTC
Re: XML Builder question (Brain Teaser? or At A Total Loss?)
On Sunday 21 August 2005 08:03 pm, Raymond Brigleb wrote:> xml.images do > -9IRDUJl30x9bj60gHklGcw@public.gmane.org do |image| > xml.image( xml.options { xml.image.option("id" => option.id, > "title" => option.title, "price" => option.price) } > "id" => image.id, > "title" => image.title, > "description" => image.description, > "category" => image.category.id, > "filename" => (image.filename+".jpg") ) > end > end > > The problem here is that I''m not sure how Builder both nests XML tags > and applies attributes to the tags. I can''t seem to find a good > example of this anywhere.Is this close to what you want? xml.images do @images.each do |image| xml.image( :id => image.id, :title => image.title, :description => image.description, :category => image.category.id, :filename => (image.filename+".jpg") ) { image.options.each do |option| xml.option( :id => option.id, :title => option.title, :price => option.price) end } end end -- -- Jim Weirich jim-Fxty1mrVU9GlFc2d6oM/ew@public.gmane.org http://onestepback.org ----------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)