开发者

What if model doesn't match controller naming?

Say I have this setup:

My model is 'content', and I want to use this model in 2 controllers.

blog_controller.rb article_controller.rb

My 'content' model has a property 'c开发者_开发百科ontent_type' which tells me if this content is a 'blog' or a 'article'.

This seems to prevent me from doing:

resources :article resources :blog

right? since the models are different? or can I still do this?


You can still do it.

In your routes file, the resources method is, by default, looking for a controller of the same name that you pass it. So regardless of what the model's name is, if you have a controller named blog_controller you can do resources :blog


In classic SO fashion, instead of answering your question I'm going to criticism your implementation ;-)

I think the root of your problem is that you want a single table for content, with code shared between articles and blogs. In rails, the way to do that is called "Single Table Inheritance".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜