Virtuous cycle

Bartlomiej Owczarek weblog

Return to 'Virtuous cycle' home page

RESTful Ogito: I need to clean up some mess

I decided to rebuild something I specifically hoped would not need to be touched, namely Ogito’s internal routes structure in Rails.

Ogito uses links looking like this:

http://www.ogito.eu/en/place/polska/warszawa/luna-marszalkowska
http://www.ogito.eu/en/directory/contentitem/movies/2003/bialy-krol-czerwony-kauczuk-czarna-smierc

The idea in itself is ok – links include context information like city and country, so that they are more meaningful.

Much better, in my opinion, than links like /places/4652. Not even from the point of view of SEO, but rather of users who are given additional hint if the link is relevant to their search or not.

However, the purpose doesn’t really justify the way I implemented them, that is by defining plenty of custom routes like this:

  map.place_edit          'place/edit/:country_id/:city_id/:place_id', :controller => 'places', :action => 'edit'
  map.place_destroy       'place/delete/:country_id/:city_id/:place_id', :controller => 'places', :action => 'destroy'
  map.place_add           'place/add/:country_id/:city_id', :controller => 'places', :action => 'new'
  map.place               'place/:country_id/:city_id/:place_id', :controller => 'places', :action => 'show'
  map.places              'places/:country_id/:city_id', :controller => 'places', :action => 'index', :defaults=>{:country_id=>nil,:city_id=>nil}

Complexity is created mainly by having countries and cities as part of the route, which has additional downside of having to provide all these objects when creating a link.

Instead I could use simple resource-based routes with some nesting, which would look more or less like this:

map.resources           :cities do |city|
    city.resources        :places, :only => [:index]
end
map.resources           :countries do |country|
    country.resources     :places, :only => [:index]
end
map.resources           :places, :except => [:index]

Nesting resources allows index links to include country or city, for example:

http://www.ogito.eu/en/countries/polska/places

I could go extreme with nesting, since for example place belongs to city which belongs to country, but I think the links would be too long as a result.

Urls of individuals objects are not nested, but they can include all the details in the id string:

http://www.ogito.eu/en/places/1161-adria-torunska-bydgoszcz

The result is the same, but there is no deviation from Rails standard and the application is much easier to develop and maintain.

Often being a novice brings benefit of fresh perspective and new ideas, but in this case I would prefer to have enough experience to get it right first time. Making changes now is not a big problem, but Google already indexed something like 3,000 subpages, so some redirection strategy will have to be in place.

Uff just hope it’s the final version this time.