Ok, I have read most of Agile Web Dev... so I had a question about beautifying URLs using routes.rb. I''m trying to architect a content management system in which a user can create template pages. This way, when a user creates a template page nested within, it displays it as though it''s displaying a folder structure, much like a directory tree (using acts_as_tree). I have a database table set-up with a page ID, page name (title), page content, parent_id, etc. My question -- I''m looking for an easy way such that the url that the app accesses mirrors a similar directory structure. So, for example... Home ^ About Us ^^^ Who we really are (nested within About Us) I would like to map this to http://localhost:3000/about-us/who-we-really-are To help with search engine rankings, and to make an easier url to remember, and it just looks prettier. This is similar to, say, how http://rails.techno-weenie.net calls the specific questions on his site. I can currently map the url correctly, however I have no way of retrieving the proper value of the id from the url to determine what page to render. My routes.rb looks as follows: map.page '':parent_name/:page_name'', :controller => ''admin'', :action => ''render_page'', :requirements => { :parent_name => /[\w-]+/, :page_name => /[\w-]+/ }, :page_name => nil Can I pass the ID of the page without actually revealing it within the URL? Or perhaps I can search the DB using LIKE by breaking up the name? -- Posted via http://www.ruby-forum.com/.
On 4/26/06, Eric Prugh <eric@eprugh.com> wrote:> Ok, I have read most of Agile Web Dev... so I had a question about > beautifying URLs using routes.rb. I''m trying to architect a content > management system in which a user can create template pages. This way, > when a user creates a template page nested within, it displays it as > though it''s displaying a folder structure, much like a directory tree > (using acts_as_tree). > > I have a database table set-up with a page ID, page name (title), page > content, parent_id, etc. My question -- I''m looking for an easy way such > that the url that the app accesses mirrors a similar directory > structure. So, for example... > > Home > ^ About Us > ^^^ Who we really are (nested within About Us) > > I would like to map this to > http://localhost:3000/about-us/who-we-really-are > > To help with search engine rankings, and to make an easier url to > remember, and it just looks prettier. This is similar to, say, how > http://rails.techno-weenie.net calls the specific questions on his site. > I can currently map the url correctly, however I have no way of > retrieving the proper value of the id from the url to determine what > page to render. > > My routes.rb looks as follows: > > map.page '':parent_name/:page_name'', :controller => ''admin'', :action => > ''render_page'', :requirements => { :parent_name => /[\w-]+/, :page_name > => /[\w-]+/ }, :page_name => nil > > > Can I pass the ID of the page without actually revealing it within the > URL? Or perhaps I can search the DB using LIKE by breaking up the name? >I''m not claiming this is a ''best-practice'' or anything, but I''ve always just put a ''short_name'' or ''url_title'' (choose your own column name as needed) attribute into the database. I create it behind the scenes in a before_save method, and do validates_uniqueness_of on it. That way it''s safe to use as a unique URL, and you can quickly do "find_by_short_name" to retrieve the instance you''re looking for. Hopefully others have better ideas, because it''s always seemed a little hackish to me, despite being very little code. --Wilson.
Wilson Bilkovich wrote:> On 4/26/06, Eric Prugh <eric@eprugh.com> wrote: >> structure. So, for example... >> http://rails.techno-weenie.net calls the specific questions on his site. >> >> Can I pass the ID of the page without actually revealing it within the >> URL? Or perhaps I can search the DB using LIKE by breaking up the name? >> > > I''m not claiming this is a ''best-practice'' or anything, but I''ve > always just put a ''short_name'' or ''url_title'' (choose your own column > name as needed) attribute into the database. > I create it behind the scenes in a before_save method, and do > validates_uniqueness_of on it. > That way it''s safe to use as a unique URL, and you can quickly do > "find_by_short_name" to retrieve the instance you''re looking for. > > Hopefully others have better ideas, because it''s always seemed a > little hackish to me, despite being very little code. > > --Wilson.I like the idea -- it doesn''t sound like too bad of a hack, and is a definite solution to my question. Thanks a lot for your help, I''ll post back if I hear or a better way. -e -- Posted via http://www.ruby-forum.com/.