A common pattern in web app development is creating a table based on some collection of objects. To make this a bit easier, I''ve implemented a CollectionTableHelper for Rails (files attached). It allows you to build a table based on any collection (Enumerable) of objects (not just AR model objects). Feel free to use and modify the code as you see fit for your projects. -- Regards, John Wilger ----------- Alice came to a fork in the road. "Which road do I take?" she asked. "Where do you want to go?" responded the Cheshire cat. "I don''t know," Alice answered. "Then," said the cat, "it doesn''t matter." - Lewis Carrol, Alice in Wonderland _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
John Wilger wrote:>A common pattern in web app development is creating a table based on >some collection of objects. To make this a bit easier, I''ve >implemented a CollectionTableHelper for Rails (files attached). It >allows you to build a table based on any collection (Enumerable) of >objects (not just AR model objects). Feel free to use and modify the >code as you see fit for your projects. > >I tried using this last week (version out of eXPlainPMT-1.0) because it''s so elegant, but am a bit stuck (new to both R and R). Content fields have to be html escaped before handing over (right?) to CTH, but I would like to use XmlMarkup. XmlMarkup will automagically escape my strings before handing over the enumerable, but then I would need a new XmlMarkup object for every string. That can''t be right, what am I missing?>From a minimizing code/processing POV it seems a possiblesolution might be to hand over the view''s xml to a modified CTH that uses the XmlMarkup object to build the table. Thanks, Russell
On 5/15/05, Russell L. Carter <rcarter-Pifbz6FK28odnm+yROfE0A@public.gmane.org> wrote:> Content fields have to be html escaped before handing over > (right?) to CTH, but I would like to use XmlMarkup. XmlMarkup > will automagically escape my strings before handing over the > enumerable, but then I would need a new XmlMarkup object > for every string. That can''t be right, what am I missing?I''m not sure that I follow what you''re trying to accomplish. Could you post a code example of what you''re trying to do? -- Regards, John Wilger ----------- Alice came to a fork in the road. "Which road do I take?" she asked. "Where do you want to go?" responded the Cheshire cat. "I don''t know," Alice answered. "Then," said the cat, "it doesn''t matter." - Lewis Carrol, Alice in Wonderland
John Wilger wrote:>On 5/15/05, Russell L. Carter <rcarter-Pifbz6FK28odnm+yROfE0A@public.gmane.org> wrote: > > >>Content fields have to be html escaped before handing over >>(right?) to CTH, but I would like to use XmlMarkup. XmlMarkup >>will automagically escape my strings before handing over the >>enumerable, but then I would need a new XmlMarkup object >>for every string. That can''t be right, what am I missing? >> >> > >I''m not sure that I follow what you''re trying to accomplish. Could you >post a code example of what you''re trying to do? > > >If you take the "Four Days on Rails" tutorial: http://www.rubyonrails.com/media/text/Rails4Days.pdf Then stuff in the example data. Next simplify and convert ~/app/views/items/list.rhtml to list.rxml: #!ruby @heading = ''List of Models'' ct = CollectionTable.new(@items, [:done], [:priority], [:description], [:due_date]) xml.form(:controller => ''items'', :action => ''new'', :method => ''post'') { xml << ct.build_table } Accessing this view via the browser results in: XML Parsing Error: not well-formed Location: http://enflex.dev.pinyon.org/ Line Number 32, Column 20: <td>Buy roses & chocolates</td> -------------------^ Because of the ampersand in the user supplied data. How to most efficiently handle this case? I''m absolutely certain I''m being stupid here, if you could be so kind, please point me in the right direction. Best regards, Russell
On 5/15/05, Russell L. Carter <rcarter-Pifbz6FK28odnm+yROfE0A@public.gmane.org> wrote:> Accessing this view via the browser results in: > XML Parsing Error: not well-formed > Location: http://enflex.dev.pinyon.org/ > Line Number 32, Column 20: <td>Buy roses & chocolates</td> > -------------------^There are a couple of solutions you can pursue. One issue is that, when you use Builder::XmlMarkup templates, Rails sends a Content-Type header of ''text/xml'' rather than ''text/html''. While technically correct, this can cause issues with some browsers. If you set up an after_filter to set the content-type to ''text/html'', you''re view will probably work fine. Other than that, you would simply have to make sure that you escape those values in the content fields before building the table. While most XmlMarkup methods do replace these character entities, the `xml <<` syntax does not. You could achieve this by using column modifiers when building the table: --- SNIP --- #!ruby @heading = ''List of Models'' ct = CollectionTable.new(@items, [:done], [:priority], [:description], [:due_date]) ct.column_modifier(:description) do |item| textilize_without_paragraph(item.description) end xml.form(:controller => ''items'', :action => ''new'', :method => ''post'') { xml << ct.build_table } --- /SNIP --- IIRC, Textile replaces these entities, so that ought to do the trick for you (assuming you have RedCloth installed). -- Regards, John Wilger ----------- Alice came to a fork in the road. "Which road do I take?" she asked. "Where do you want to go?" responded the Cheshire cat. "I don''t know," Alice answered. "Then," said the cat, "it doesn''t matter." - Lewis Carrol, Alice in Wonderland