开发者

Usage of MVC in convenient way in Rails

I have an article controller, which manages all article relations like authors, translations, related articles and so on in one update action at this moment.

Page contains a bunch of "Listings" with search and sort capabilities,CRD actions, forms for creating new and manage existing association records.

All this stuff is managed by ArticleController edit and update actions and bunch of partial templates for each listing.

I want to extend functionality more by adding additional search tables to find records for association. And later I'll add some other features.

I feel that this way (all association management in one edit page) is far far from convenient. So, maybe, better is to create some special controllers for each listing like:

/articles/1/edit/authors

And what should be the name of this controller? Should it be children of ArticleController or PeopleController or ApplicationController?

or maybe all okay and I am paranoiac?)

UPD

All MVC conditions are sctictly met. Question is:

Is it good to manage all relations in one controller. Not only add/remove relation, sometimes creating a newone, like @article.authors.find_or_create_by_name(name). Relations in view are not simply select tags! They are fullyfeachured tables with own search, sort, paginate capabilities.

If someone dont understand me, lets ask in different way:

how to create a "subcontroller": if we have ArticleController and its edit action. ArticleController should manage only fields in Article model. And a bunch of subcontrollers should manage relations.

Why I am need it? Lets add a AJAX to such page: no serious problems are met, but on every AJAX call ArticleController.update/edit actions a query and thay may do some unnessesary work like building Arel relations for models, that will never be used. I like to divide work into small independent pieces, not to bu开发者_运维技巧ild a one huge Controller, that can do everything.


You article controller shouldn't manage relations. That should be in your model. Look into active record callbacks. You can do everything related to managing your authors, translations, etc in a callback. For example, in a after_update callback

You also want to be using nested resources. For example, your authors are nested inside your articles


Maybe you can, for instance, have an Authorship controller that will manage the author <-> article relationship. Don't forget that you can use the "join" model to handle the relation between the 2 models involved. You could have routes like

edit_article_authorships GET /articles/:article_id/authorships/edit(.:format) {:controller=>"authorships", :action=>"edit"}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜