Is it possible to define a route that matches on a regular expression? I want to take only the pages that end in a 4 digit number, in any controller/action in the entire site. http://mydomain.com/asdfasdf/1234 <- matches http://mydomain.com/1234 <- matches http://mydomain.com/asdfasdf/morestuff/1234 <- matches http://mydomain.com/123a <- doesn''t match Anybody have any alternative suggestions on accomplishing this? -Jeff _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
you can make sure that a parameter matches a certain format. http://mydomain.com/:year, :year => /\d{4}/ will match mydomain.com/2004 but not mydomain/50 There is no way to do something with the last parameter. If you can''t make a handful of real rules out of it you can use mod_rewrite to move numerical last parameters into the beginning of the url or into the query string, your pick. On Apr 6, 2005 7:33 PM, Jeffrey Moss <jeff-61uStg5MtoFWk0Htik3J/w@public.gmane.org> wrote:> > Is it possible to define a route that matches on a regular expression? > > I want to take only the pages that end in a 4 digit number, in any > controller/action in the entire site. > > http://mydomain.com/asdfasdf/1234 <- matches > http://mydomain.com/1234 <- matches > http://mydomain.com/asdfasdf/morestuff/1234 <- matches > http://mydomain.com/123a <- doesn''t match > > Anybody have any alternative suggestions on accomplishing this? > > -Jeff > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Tobi http://www.snowdevil.ca - Snowboards that don''t suck http://www.hieraki.org - Open source book authoring http://blog.leetsoft.com - Technical weblog
On Apr 6, 2005 6:01 PM, Tobias Luetke <tobias.luetke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> There is no way to do something with the last parameter. If you can''t > make a handful of real rules out of it you can use mod_rewrite to move > numerical last parameters into the beginning of the url or into the > query string, your pick.Hmmm, i''m not so sure. I remember there''s a new feature in rails that you can somehow get an array that contains each "subdirectory" in the URL, though I forget how to do this. If you did that, you could do some kind of processing on the array index "-1", which would be the last element, no matter how many subdirectories. -- Urban Artography http://artography.ath.cx
On 7.4.2005, at 08:25, Rob Park wrote:> Hmmm, i''m not so sure. I remember there''s a new feature in rails that > you can somehow get an array that contains each "subdirectory" in the > URL, though I forget how to do this. If you did that, you could do > some kind of processing on the array index "-1", which would be the > last element, no matter how many subdirectories.Please, someone more knowledgeable about this, share with us :-) I talked with Ulysses about a feature like this a few weeks ago but didn''t know it got implemented already. //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hi! On Thu, 07 Apr 2005, Jarkko Laine wrote the following:> Please, someone more knowledgeable about this, share with us :-) > > I talked with Ulysses about a feature like this a few weeks ago but > didn''t know it got implemented already.hmm, I think you''re talking about ''path_info'', don''t you? see http://dev.rubyonrails.com/ticket/883 ---- routes.rb map.connect ''something/*path_info'', :controller => ''something'', :action => ''list'' ---- ---> access trough "@params[''path_info'']" in your controller kind regards Wolfgang
Jeffrey Moss wrote:> Is it possible to define a route that matches on a regular expression? > > I want to take only the pages that end in a 4 digit number, in any > controller/action in the entire site. > > http://mydomain.com/asdfasdf/1234 <- matches > http://mydomain.com/1234 <- matches > http://mydomain.com/asdfasdf/morestuff/1234 <- matches > http://mydomain.com/123a <- doesn''t match > > Anybody have any alternative suggestions on accomplishing this?Like Wolgang already said you could use path_info. If you want to match anywhere you could do somthing like that. # routes.rb map.connect ''*path_info'', :controller => ''main'', :action => ''route'' This will not allow other routes I think (untested), but you are able to handle the routing yourself in your MainController. class MainController def route pi = @params[''path_info''] # do whatever you want with your request # you are essentially being responmsible # for your own request dispatching now end end I would probably use a prefix, so that it would still be possible to use the routing. # routes.rb map.connect ''main/*path_info'', :controller => ''main'', :action => ''route'' Sascha
What would be nice is a regex method that replaces mod_rewrite completely. mod_rewrite doesn''t have the same powers as the perl compatible regex''s in ruby. -Jeff ----- Original Message ----- From: "Sascha Ebach" <se-eFwX6J65rk9VioaHkBSlcw02NpfuEekPhC4ANOJQIlc@public.gmane.org> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> Sent: Thursday, April 07, 2005 4:05 AM Subject: Re: [Rails] Regular Expressions in Routes> Jeffrey Moss wrote: >> Is it possible to define a route that matches on a regular expression? >> I want to take only the pages that end in a 4 digit number, in any >> controller/action in the entire site. >> http://mydomain.com/asdfasdf/1234 <- matches >> http://mydomain.com/1234 <- matches >> http://mydomain.com/asdfasdf/morestuff/1234 <- matches >> http://mydomain.com/123a <- doesn''t match >> Anybody have any alternative suggestions on accomplishing this? > > Like Wolgang already said you could use path_info. If you want to match > anywhere you could do somthing like that. > > # routes.rb > map.connect ''*path_info'', :controller => ''main'', :action => ''route'' > > This will not allow other routes I think (untested), but you are able to > handle the routing yourself in your MainController. > > class MainController > def route > pi = @params[''path_info''] > # do whatever you want with your request > # you are essentially being responmsible > # for your own request dispatching now > end > end > > I would probably use a prefix, so that it would still be possible to use > the routing. > > # routes.rb > map.connect ''main/*path_info'', :controller => ''main'', :action => ''route'' > > Sascha > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Jeffrey Moss wrote:> What would be nice is a regex method that replaces mod_rewrite > completely. mod_rewrite doesn''t have the same powers as the perl > compatible regex''s in ruby.Hm, I don''t understand. The method I explained earlier completely replaces the routing. In the #route method you can do whatever you want with the pathinfo information. You have the full power of Ruby at your disposal. Sascha
Well, I can''t use a prefix because of legacy issues, what I want is to catch this 4 digit number at the end of any path and convert it into a parameter, it seems like this would best be implemented as a "filter" regex for routes, which is called on every pass but does not map to any one action, much like the filters for controllers. like this: map.rewrite /^(.*)([0-9]{4})$/, $1, :agent_id => $2 map.connect '':controller/:action/:id'' How would I go about re-dispatching an action from within my route handling controller? -Jeff ----- Original Message ----- From: "Sascha Ebach" <se-eFwX6J65rk9VioaHkBSlcw02NpfuEekPhC4ANOJQIlc@public.gmane.org> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> Sent: Thursday, April 07, 2005 10:08 AM Subject: Re: [Rails] Regular Expressions in Routes> Jeffrey Moss wrote: >> What would be nice is a regex method that replaces mod_rewrite >> completely. mod_rewrite doesn''t have the same powers as the perl >> compatible regex''s in ruby. > > Hm, I don''t understand. The method I explained earlier completely replaces > the routing. In the #route method you can do whatever you want with the > pathinfo information. You have the full power of Ruby at your disposal. > > Sascha > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Jeffrey Moss wrote:> Well, I can''t use a prefix because of legacy issues, what I want is to > catch this 4 digit number at the end of any path and convert it into a > parameter, it seems like this would best be implemented as a "filter" > regex for routes, which is called on every pass but does not map to any > one action, much like the filters for controllers.Each routing rule is the equivalent of a mod_rewrite [L] (last rule) I need to extract optional information from every URL, which means I need two copies of every route.> like this: > map.rewrite /^(.*)([0-9]{4})$/, $1, :agent_id => $2 > map.connect '':controller/:action/:id''map.route /:agent_id$/, :agent_id => /\d{4}/ map.route /^:lang/, :lang => /en|de|fr|ja/ map.connect ... Routes make a chain leading to the destination. To find a controller, follow the routes to a connection. To generate a URL from a controller, start from the connection and follow the routes. jeremy
Sorry Jeremy, I don''t understand what you''re saying here. What do you mean by two copies of every route? I don''t see where you''re doing that. I put your map.route line directly in above my map.connect ? I thought map.connect was an alias for map.route? -Jeff ----- Original Message ----- From: "Jeremy Kemper" <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> Sent: Thursday, April 07, 2005 11:31 AM Subject: Re: [Rails] Regular Expressions in Routes> Jeffrey Moss wrote: >> Well, I can''t use a prefix because of legacy issues, what I want is to >> catch this 4 digit number at the end of any path and convert it into a >> parameter, it seems like this would best be implemented as a "filter" >> regex for routes, which is called on every pass but does not map to any >> one action, much like the filters for controllers. > > Each routing rule is the equivalent of a mod_rewrite [L] (last rule) > > I need to extract optional information from every URL, which means I > need two copies of every route. > >> like this: >> map.rewrite /^(.*)([0-9]{4})$/, $1, :agent_id => $2 >> map.connect '':controller/:action/:id'' > > map.route /:agent_id$/, :agent_id => /\d{4}/ > map.route /^:lang/, :lang => /en|de|fr|ja/ > > map.connect ... > > Routes make a chain leading to the destination. To find a controller, > follow the routes to a connection. To generate a URL from a controller, > start from the connection and follow the routes. > > jeremy > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Jeffrey Moss wrote:> Sorry Jeremy, I don''t understand what you''re saying here. > > What do you mean by two copies of every route? I don''t see where you''re > doing that. > > I put your map.route line directly in above my map.connect ? I thought > map.connect was an alias for map.route?Sorry, this isn''t how routes work today. I have the same problem as you (need to pull info from every route) so I suggested a nice way to deal with it. I''m not sure how to implement it efficiently. Algorithms experts? jeremy