Sorry for asking what is probably the bleeding obvious to you pros, but I can''t seem to work this out (and yes Ive read the routing HowTo as well). I have a CMS. It has documents that belong to sections. Currently to view a document, the url looks like http://127.0.0.1:3000/document/show/1 - where document is the controller, show is the action, and 1 is the documents'' id in the documents table. The above document belongs_to a section, which has an id and a name (i.e. "products") in the sections table. I would like my urls to look something like: http://127.0.0.1:3000/products/large-sized-widget where /products/ in the url above relates to document.section.name (the name column in the section table), and "large-size-widget" relates to document.menu_name (the menu_name column in the documents table) Am I going about this whole thing the right way? Can somebody advise me of the correct routing code I should be using, and any other controller-specific code required to get this all working. (one more thing - is there some nice and easy string manipulation code i can use to turn a menu name like "large sized widget" into "large-sized-widget") - as much as I''m loving Rails, I''m not find the documentation particularly helpful, so I apologise in advance for these elementary questions thanks Robbie
On 12.5.2005, at 12:04, Robbie wrote:> Sorry for asking what is probably the bleeding obvious to you pros, > but I can''t > seem to work this out (and yes Ive read the routing HowTo as well). > > I have a CMS. It has documents that belong to sections. > > Currently to view a document, the url looks like > http://127.0.0.1:3000/document/show/1 - where document is the > controller, show > is the action, and 1 is the documents'' id in the documents table. > > The above document belongs_to a section, which has an id and a name > (i.e. > "products") in the sections table. > > I would like my urls to look something like: > > http://127.0.0.1:3000/products/large-sized-widget > > where /products/ in the url above relates to document.section.name > (the name > column in the section table), and "large-size-widget" relates to > document.menu_name (the menu_name column in the documents table) > > Am I going about this whole thing the right way? Can somebody advise > me of the > correct routing code I should be using, and any other > controller-specific code > required to get this all working.map.connect '':section/:page'', :controller => pages, :action => ''show'' Remember that this clashes with the default route so you need to use some kind of requirements with one or both rules: map.connect '':section/:page'', :controller => pages, :action => ''show'', :requirements => { :section => /section_a|section_b|section_c/ } map.connect '':controller/:action'', :requirements => { :controller => /onecontroller|othercontroller|thirdcontroller/ } //jarkko> > (one more thing - is there some nice and easy string manipulation code > i can use > to turn a menu name like "large sized widget" into > "large-sized-widget") - as > much as I''m loving Rails, I''m not find the documentation particularly > helpful, > so I apologise in advance for these elementary questions > > thanks > Robbie > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hey Robbie, I''m no Routes expert, but I''m thinking of a solution something like this: Change your document controller and show action to accept the combination of document.section.name and document.menu_name (I''m assuming that these two columns make a unique index, equivalent to the id column). So, you''d need to be able to access your documents like this: http://127.0.0.1:3000/document/show/?section=products&menu_name=large- sized-widget Once this is working, you should be able to set up your route like this: map.connect ":section/:menu_name", :controller => "document", :action => "show" Although this might be ambiguous since there is no way to uniquely identify this route. Maybe you can do something like this: map.connect "catalog/:section/:menu_name", :controller => "document", :action => "show" Duane Johnson (canadaduane) On May 12, 2005, at 3:04 AM, Robbie wrote:> Sorry for asking what is probably the bleeding obvious to you pros, > but I can''t > seem to work this out (and yes Ive read the routing HowTo as well). > > I have a CMS. It has documents that belong to sections. > > Currently to view a document, the url looks like > http://127.0.0.1:3000/document/show/1 - where document is the > controller, show > is the action, and 1 is the documents'' id in the documents table. > > The above document belongs_to a section, which has an id and a name > (i.e. > "products") in the sections table. > > I would like my urls to look something like: > > http://127.0.0.1:3000/products/large-sized-widget > > where /products/ in the url above relates to document.section.name > (the name > column in the section table), and "large-size-widget" relates to > document.menu_name (the menu_name column in the documents table) > > Am I going about this whole thing the right way? Can somebody > advise me of the > correct routing code I should be using, and any other controller- > specific code > required to get this all working. > > (one more thing - is there some nice and easy string manipulation > code i can use > to turn a menu name like "large sized widget" into "large-sized- > widget") - as > much as I''m loving Rails, I''m not find the documentation > particularly helpful, > so I apologise in advance for these elementary questions > > thanks > Robbie > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
> (one more thing - is there some nice and easy string manipulation > code i can use > to turn a menu name like "large sized widget" into "large-sized- > widget") - as > much as I''m loving Rails, I''m not find the documentation > particularly helpful, > so I apologise in advance for these elementary questionsHow about: menu_name.gsub " ", "-" ? Duane
On 12.5.2005, at 14:22, Duane Johnson wrote:>> (one more thing - is there some nice and easy string manipulation >> code i can use >> to turn a menu name like "large sized widget" into >> "large-sized-widget") - as >> much as I''m loving Rails, I''m not find the documentation particularly >> helpful, >> so I apologise in advance for these elementary questions > > How about: > > menu_name.gsub " ", "-"I have this in my application helper: def sanitize(str) str.gsub(/[^\w\s-]/, '''').gsub(/[\s]/, ''-'').downcase end It strips all illegal characters and replaces spaces with dashes. //jarkko> > ? > > Duane > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 12/05/2005, Robbie wrote: Hello Robbie,> (one more thing - is there some nice and easy string manipulation code i can use > to turn a menu name like "large sized widget" into "large-sized-widget") - as > much as I''m loving Rails, I''m not find the documentation particularly helpful, > so I apologise in advance for these elementary questionsSince it''s not Rails related but Ruby related you should have a look at: - http://whytheluckystiff.net/ruby/pickaxe/ : for method explanations and language usage informations - http://www.ruby-doc.org/stdlib/ : for the standard library documentation For your problem, the solution is "gsub" method (http://whytheluckystiff.net/ruby/pickaxe/html/ref_c_string.html#String.gsub) Have a good day. -- Nicolas Cavigneaux | GPG KeyID : CFE76D24 nico-DRabjd/C3MEdnm+yROfE0A@public.gmane.org | http://www.bounga.org _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 5/12/05, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote:> I have this in my application helper: > > def sanitize(str) > str.gsub(/[^\w\s-]/, '''').gsub(/[\s]/, ''-'').downcase > end > > It strips all illegal characters and replaces spaces with dashes.The problem here is that you need to be able to reverse it in order to get from the sanitized version in the URL to the actual value that is in the database. -- Urban Artography http://artography.ath.cx
On 12.5.2005, at 23:01, Rob Park wrote:> On 5/12/05, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote: >> I have this in my application helper: >> >> def sanitize(str) >> str.gsub(/[^\w\s-]/, '''').gsub(/[\s]/, ''-'').downcase >> end >> >> It strips all illegal characters and replaces spaces with dashes. > > The problem here is that you need to be able to reverse it in order to > get from the sanitized version in the URL to the actual value that is > in the database.No no, I use it to create url''s from page or blog post (or whatever) names. The url is then saved in the db, too. //jarkko> > -- > Urban Artography > http://artography.ath.cx > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 5/12/05, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote:> On 12.5.2005, at 23:01, Rob Park wrote: > > The problem here is that you need to be able to reverse it in order to > > get from the sanitized version in the URL to the actual value that is > > in the database. > > No no, I use it to create url''s from page or blog post (or whatever) > names. The url is then saved in the db, too.Oh, so you''re using this function to populate a field in the db, that is then later used to retreive that entry from the db? Ok, that''s a bit smarter. I was thinking that it would be called each time a page loads to create the URLs. -- Urban Artography http://artography.ath.cx
> Oh, so you''re using this function to populate a field in the db, that > is then later used to retreive that entry from the db? Ok, that''s a > bit smarter. I was thinking that it would be called each time a page > loads to create the URLs.We used this technique at http://www.bellybutton.de and took an Observer to generate a ''url_safe_name'' each time a record was created or updated. So we could easiely implement this feature to many models. The Observer also cheked, if a URL was already used by another record and appended a number if needed. -- Norman Timmler Holländische Reihe 31 22765 Hamburg +49 (0)40 / 43 25 10 80 mailto:norman-QkIQCVqxERM@public.gmane.org _ ASCII ribbon campaign ( ) - against HTML email X & vCards / \