I am using Rails single-table inheritance with a superclass Content and a number of subclasses (such as Article, Comment etc.) I am trying to get away with a single controller, and I have set up the routes thusly:
resources :contents
resources :articles, :controller => "contents"
resources :comments, :controller =>开发者_运维技巧 "contents"
This way, /articles/new
gets routed to contents#new
which seems to be what I want.
Within the controller and the views, however, I need to tailor the functionality a bit depending on which actual model I am dealing with. For that purpose, I need to determine the original requested resource or otherwise find out which subclass I am dealing with.
Looking at params
for /articles/new
within the common controller gives {"action"=>"new", "controller"=>"contents"}
, which obviously does not provide the information I need.
Either the answer is really obvious or I am using model inheritance wrong. Which one is it? :)
You can extract the part of the request path you are interested in like this
path = request.fullpath[%r{^/(articles|comments)/}, 1] # articles or comments
Once you have it you can get the model class like that:
model_class = path.classify.constantize # Article or Comment
Bests,
Richard
精彩评论