Dynamic Routing

Hi,
I want to have the following URL structure
/music
/movies
etc. for categories of the site.

/about
/help
etc. for static pages.

Both types auf URLs should be generated dynamically.

But how do I route them?

map.connect ‘/:category/’, :controller=>‘magazine’, :action=>‘index’,
:rubrik => // #should be generated from database, but how?

map.connect ‘/:page/’, :controller=>‘magazine’, :action=>‘page’,
:page => /[(help)|(about)]/ #but should also be generated
dynamically

Thanks.

Sounds interesting.
But I’ve already found a simpler solution, without making a plugin:

map.connect ‘/’, :controller =>‘magazin’, :action=>‘index’
map.connect ‘/:rubrik/’, :controller=>‘magazin’, :action=>‘index’,
:rubrik => Regexp.new(Category.find(:all).collect {|o|
“(#{o.name})”}.join(‘|’).downcase)

map.connect ‘/:page/’, :controller=>‘magazin’, :action=>‘page’,
:page => Regexp.new(Page.find(:all).collect {|o|
“(#{o.title})”}.join(‘|’).downcase)

map.slug ‘/:slug/’, :controller =>‘magazin’, :action=>‘artikel’

You can create an Regexp from an array. Great. But maybe slow?

nice regards,
ernst

On 2/25/07, Bojan M. [email protected] wrote:

etc. for static pages.

best regards,


wer viel lest ist viel gebildet.

Ernst Beiglböck wrote:

Both types auf URLs should be generated dynamically.

But how do I route them?

map.connect ‘/:category/’, :controller=>‘magazine’, :action=>‘index’,
:rubrik => // #should be generated from database, but how?

map.connect ‘/:page/’, :controller=>‘magazine’, :action=>‘page’,
:page => /[(help)|(about)]/ #but should also be generated dynamically

Thanks.

http://www.aflatter.de/typo/articles/2007/01/14/dynamic-routing-in-rails

I run into one article when looking for a way to implement routes from
database and multi-language URL’s. It could be of interest to you, I
didn’t try it yet:

http://www.aflatter.de/typo/articles/2007/01/14/dynamic-routing-in-rails

best regards,
Bojan


Bojan M.
Informatika Mihelac, Bojan M. s.p. | www.informatikamihelac.com
→ tools, scripts, tricks from our code lab: http://source.mihelac.org

Hi –

On 2/25/07, Phlip [email protected] wrote:

etc. for static pages.
assert_routing ‘/mr_smith’,
:controller => ‘account’,
:action => ‘home_page’,
:login => ‘mr_smith’

Now you just read a *param in your controller, and render the appropriate
response.

I think you probably want :login, rather than *login. *login gives you
a route glob, which might contain any number of fields. They’ll be
delivered to the action in an array (params[:login]), whereas with
:login you’ve got just one wildcard field, which will be delivered as
a string.

There’s a bit of a potential problem with this route, though. If
someone has a login name that’s the same as a controller, you might be
in trouble:

ActionController::Routing::Routes.recognize_path(“/dblack”)
=> {:login=>“dblack”, :controller=>“account”, :action=>“home_page”}
ActionController::Routing::Routes.recognize_path(“/questions”)
=> {:controller=>“questions”, :action=>“index”}

If someone happens to sign up as “questions”, their home page link
won’t work. So it might be better to do something like:

map.connect ‘home/:login’, :controller => “account”, :action =>
“home_page”

which gives you:

ActionController::Routing::Routes.recognize_path(“/home/david”)
=> {:login=>“david”, :controller=>“account”, :action=>“home_page”}
ActionController::Routing::Routes.recognize_path(“/home/questions”)
=> {:login=>“questions”, :controller=>“account”, :action=>“home_page”}

David


Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

Ernst Beiglböck wrote:

I want to have the following URL structure
/music
/movies
etc. for categories of the site.

/about
/help
etc. for static pages.

This provides a custom homepage for any user: site.com/my_name

map.connect ‘:controller/:action/:id’
map.connect ‘*login’, :controller => ‘account’, :action => ‘home_page’

The trick is that param[:login] arrives in your controller.

I suspect I should have tested my system with this:

assert_routing '/mr_smith',
               :controller => 'account',
               :action => 'home_page',
               :login => 'mr_smith'

Now you just read a *param in your controller, and render the
appropriate
response.


Phlip
Redirecting... ← NOT a blog!!!

Ernst Beiglböck wrote:

“(#{o.title})”}.join(‘|’).downcase)

map.slug ‘/:slug/’, :controller =>‘magazin’, :action=>‘artikel’

You can create an Regexp from an array. Great. But maybe slow?

nice regards,
ernst

Do you reload routes when category or page is added in database?

best regards,
Bojan


Bojan M.
Informatika Mihelac, Bojan M. s.p. | www.informatikamihelac.com
→ tools, scripts, tricks from our code lab: http://source.mihelac.org

I didn’t think of it, but now as you say it I think I better should :wink:

On 2/26/07, Bojan M. [email protected] wrote:

map.connect ‘/:page/’, :controller=>‘magazin’, :action=>‘page’,


wer viel lest ist viel gebildet.