Forum: Ruby on Rails STI and routing

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community 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
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
Wally Valters (Guest)
on 2008-12-16 20:02
(Received via mailing list)
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)
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.