Forum: Ruby on Rails STI and routing

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Mr C. (Guest)
on 2008-12-16 11:28
(Received via mailing list)
Hello all,

I have a main model Article, which is subclassed by several others
like so:

class Music < Article
  class RecordReview < Music; end
  class Interview < Music; end
class Film < Article
  class Review < Film; end
  class Interview < Film; end

Doing it this way I can simply call Music.all, or
Music::RecordReview.all etc to find relevant articles. For simplicity
I've decided to use only one controller ArticlesController. My URL
schema is like this:


So - my question is - does anybody know a clean way to define this in
routes.rb, bearing in mind that all requests need to be passed to the
ArticlesController, which then needs to determine which type of
article we're currently requesting.

Right now i'm just doing something like this:

map.connect ':section/:subsection/:id', :controller =>
'articles', :action => 'show'

and then I use a before_filter to determine the type from the :section
and :subsection params - which works - but I then don't get any of the
url/path helpers generated doing a map.resources so things are getting
a bit messy.

Thanks in advance
Wally Valters (Guest)
on 2008-12-16 20:02
(Received via mailing list)
If you name the route you would...

like:  ':section/:subsection/:id', :controller =>music', :action
=> 'show'

then you could use
(:action=>'whatever', :section=>'section', :subsection=>'subsection',
Mr C. (Guest)
on 2008-12-16 22:31
(Received via mailing list)
Thanks Wally. Think this could work...
This topic is locked and can not be replied to.