STI and routing


#1

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
end
class Film < Article
class Review < Film; end
class Interview < Film; end
end
…etc

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:


‘/music’
‘/music/record_reviews’
‘/music/record_reviews/1’
‘/music/record_reviews/new’
‘/music/record_reviews/1/edit’
‘/film’
‘/film/reviews’
…etc

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’
…etc

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


#2

If you name the route you would…

like:
map.music ‘:section/:subsection/:id’, :controller =>music’, :action
=> ‘show’

then you could use
music_url
(:action=>‘whatever’, :section=>‘section’, :subsection=>‘subsection’,
:id=>1)


#3

Thanks Wally. Think this could work…