A part of a homebrewed content management system that I''m trying to convert to RoR, I have relative URLs in the form: "page$2" embedded in a text field in the database. When displayed on a page, they look like this: http://hostname/page/page$2 I need them to look like this: http://hostname/page/2 My controller is "page", obviously, and the record ID is 2, so I''m part way there. I know I can do it via a rewrite rule in .htaccess, but can it be done in with a route? If so, how? I''m new to Rails and have managed to wrap my head around routes for simple cases, but having figured out how to extract specific values. Thanks for any help you can lend. Tom ------------------------------------------------------------------------ ---- Tom Donovan Hawthorn School District 73 Mgr. of Tech. Systems, Webmaster Vernon Hills, IL ------------------------------------------------------------------------ ----
something like: map.connect ''page/:id'', :controller => ''page'', :action => ''view'' should work fine for what you''d want I reckon. -johan On Sat, 19 Mar 2005 08:55:20 -0600, Tom Donovan <donovant-jG4xwPahMbbSppZulB+HuQ@public.gmane.org> wrote:> A part of a homebrewed content management system that I''m trying to > convert to RoR, I have relative URLs in the form: "page$2" embedded in > a text field in the database. When displayed on a page, they look like > this: > > http://hostname/page/page$2 > > I need them to look like this: > > http://hostname/page/2 > > My controller is "page", obviously, and the record ID is 2, so I''m part > way there. I know I can do it via a rewrite rule in .htaccess, but can > it be done in with a route? If so, how? I''m new to Rails and have > managed to wrap my head around routes for simple cases, but having > figured out how to extract specific values. > > Thanks for any help you can lend. > > Tom > > ------------------------------------------------------------------------ > ---- > Tom Donovan Hawthorn School District 73 > Mgr. of Tech. Systems, Webmaster Vernon Hills, IL > ------------------------------------------------------------------------ > ---- > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Thanks, Johan. That got me closer than I had been, but still not quite there. Rails is taking the request (/page/page$2) and interpreting "page$2" as the ID, which causes an "ActiveRecord::RecordNotFound" error, not surprisingly. What I need to be able to do, I''m now seeing, is parse the request "object", extracting the "2". In the ActionController docs, I found a method "path_info" that looks promising, but I''m not sure how I would use it within the context of routes.rb. How would I reference the request object? Tom On Mar 19, 2005, at 8:59 AM, Johan Sörensen wrote:> something like: > > map.connect ''page/:id'', :controller => ''page'', :action => ''view'' > > should work fine for what you''d want I reckon. > > -johan > > > On Sat, 19 Mar 2005 08:55:20 -0600, Tom Donovan > <donovant-jG4xwPahMbbSppZulB+HuQ@public.gmane.org> wrote: >> A part of a homebrewed content management system that I''m trying to >> convert to RoR, I have relative URLs in the form: "page$2" embedded >> in >> a text field in the database. When displayed on a page, they look >> like >> this: >> >> http://hostname/page/page$2 >> >> I need them to look like this: >> >> http://hostname/page/2 >> >> My controller is "page", obviously, and the record ID is 2, so I''m >> part >> way there. I know I can do it via a rewrite rule in .htaccess, but >> can >> it be done in with a route? If so, how? I''m new to Rails and have >> managed to wrap my head around routes for simple cases, but having >> figured out how to extract specific values. >> >> Thanks for any help you can lend. >> >> Tom >> >> ---------------------------------------------------------------------- >> -- >> ---- >> Tom Donovan Hawthorn School District 73 >> Mgr. of Tech. Systems, Webmaster Vernon Hills, IL >> ---------------------------------------------------------------------- >> -- >> ---- >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Sat, 19 Mar 2005 10:25:57 -0600, Tom Donovan <donovant-jG4xwPahMbbSppZulB+HuQ@public.gmane.org> wrote:> Thanks, Johan. That got me closer than I had been, but still not quite > there. Rails is taking the request (/page/page$2) and interpreting > "page$2" as the ID, which causes an "ActiveRecord::RecordNotFound" > error, not surprisingly.I''d probably use mod_rewrite in this case. -- miles
On 19.3.2005, at 18:30, Miles Egan wrote:> On Sat, 19 Mar 2005 10:25:57 -0600, Tom Donovan > <donovant-jG4xwPahMbbSppZulB+HuQ@public.gmane.org> wrote: >> Thanks, Johan. That got me closer than I had been, but still not >> quite >> there. Rails is taking the request (/page/page$2) and interpreting >> "page$2" as the ID, which causes an "ActiveRecord::RecordNotFound" >> error, not surprisingly. > > I''d probably use mod_rewrite in this case.I wouldn''t. You can extract the number from the id ("page$2" in this case) in your action: foo, id = id.split(''$'') //jarkko> > -- > miles > _______________________________________________ > 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 Mar 19, 2005, at 11:09 AM, Jarkko Laine wrote:> > I wouldn''t. You can extract the number from the id ("page$2" in this > case) in your action: > > foo, id = id.split(''$'') > > //jarkko >Thanks for putting me on the right track, Jarkko. I got it to work by doing this in my action: def show foo, id = @params[''id''].split(''$'') if foo == ''page'' redirect_to :controller => ''page'', :action => ''show'', :id => id else @page = Page.find(@params[''id'']) end end This seems kind of brute-force, but it works. Is there a more efficient way to accomplish this (that is, redirecting a request in the form "page$2" to "page/2")? Tom