Hi All, I have been learning some ruby and some Camping at the same time by implementing a little webapp to track expenses (one man''s blog...) that includes the ability to tag entries with 1 or more tags. I wanted to offer the possibility to narrow the view of expenses by adding tags to a filter (this works) and I also wanted this filter to be reflected in the URL. Like so: normal URL: http://localhost:3301/ filtered URL: http://localhost:3301/filter/tag1/tag2/tag3 However, I can''t get this to work since I require a variable number of regexp groups (like say an array) and I could not find a routing syntax that would allow this (something like class Filter < R ''/filter/(\w/)*'' ). I currently use a route which looks like: class Filter < R ''/filter'', ''/filter/(.+)'' And I represent the N tags as a string where tags are comma-separated. It works but purty, it isn''t. Suggestions? Or am I bonkers? Thanks. -- Boris -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/camping-list/attachments/20061114/2046eba9/attachment.html
This works for me: class Filter < R ''/filter/([\w/]+)'' def get tags @tags = tags.split(''/'') end end Then http://localhost:3301/filter/tag1/tag2/tag3 will make @tags into [''tag1'', ''tag2'', ''tag3'']. I think this is what you want... Roland On Nov 14, 2006, at 3:42 PM, Boris Terzic wrote:> Hi All, > > I have been learning some ruby and some Camping at the same time by > implementing a little webapp to track expenses (one man''s blog...) > that includes the ability to tag entries with 1 or more tags. > > I wanted to offer the possibility to narrow the view of expenses by > adding tags to a filter (this works) and I also wanted this filter > to be reflected in the URL. Like so: > > normal URL: http://localhost:3301/ > filtered URL: http://localhost:3301/filter/tag1/tag2/tag3 > > However, I can''t get this to work since I require a variable number > of regexp groups (like say an array) and I could not find a routing > syntax that would allow this (something like class Filter < R ''/ > filter/(\w/)*'' ). > > I currently use a route which looks like: > > class Filter < R ''/filter'', ''/filter/(.+)'' > > And I represent the N tags as a string where tags are comma- > separated. It works but purty, it isn''t. > > Suggestions? Or am I bonkers? > > Thanks. > > -- > Boris
On Nov 14, 2006, at 9:42 PM, Boris Terzic wrote:> However, I can''t get this to work since I require a variable number > of regexp groups (like say an array) and I could not find a routing > syntax that would allow this (something like class Filter < R ''/ > filter/(\w/)*'' ).Correct.> And I represent the N tags as a string where tags are comma- > separated. It works but purty, it isn''t. > > Suggestions? Or am I bonkers?Well, you''re obviously bonkers. Who in their right mind would ever start programming Ruby? AFAIK there is no way to represent an arbitrary number of arguments in a URL in camping, but if you stop to think what a URL is then it''s not so strange. The most obvious place to put parts of a URL that don''t point to a resource is the query string. < R ''/expenses'' /expenses?filter=tag1+tag2+tag3 Manfred
Hi Boris, welcome to Ruby and Camping :) Camping doesn''t support nested regexp groups like ((\w+)\/) so you''ll have to do it differently. Your second approach is right. You can also use the multiple arguments input if you don''t want to split the string with the commas. Like ?tag=one&tag=two&tag=three. That will give you a [''one'', ''two'', ''three''] array in input.tag. -- Cheers, zimbatm http://zimbatm.oree.ch
First off thanks for all the great responses! And now some individual comments. @Manfred: Roland''s solution does seems to allow this approach, and while it is not entirely "clean" it''s no worse than my comma-separated-directory approach. As for these sorts of queries not representing resources and perhaps belonging into a query string? I don''t know, I get your point but then again "expenses/filter/tag1/tag2/tag3" does represent the resource that is the collection of all expenses that have those tags. Maybe. This is where you need to get Sam Ruby involved for a restful knock on the head. @Roland: cool, I will adopt it as long as I don''t get head-bashed by Sam. @Jonas: thanks for the welcome, and yet another approach! Interesting feature with the array parameter. Camping rocks. (yes, I am a fanboy) Cheers, Boris -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/camping-list/attachments/20061121/7bcf8452/attachment.html