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.

Ernst Beiglböck wrote:

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.

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

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

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.

Hi --

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.

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

Ernst Beiglböck wrote:

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

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

best regards, Bojan

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