简体   繁体   English

优化重复的Rails代码的正确方法

[英]Correct way to optimize repeated Rails code

I have a Rails application with several models-views-controllers which have some similar characteristics, for example 5 different models can be commented on, voted on or tagged, I am also heavily using external plugins. 我有一个带有几个具有类似特征的models-views-controllers的Rails应用程序,例如可以对5个不同的模型进行评论,投票或标记,我也大量使用外部插件。

At the moment I introduced comments, votes, tags, etc. only to a single model (and its view and controller). 目前,我仅向单个模型(及其视图和控制器)介绍评论,投票,标签等。 However, now that I am happy with the results, I want to cut out this common functionality from the particular MVC of one model and allow access to it from all other models. 但是,现在我对结果感到满意,所以我想从一个模型的特定MVC中删除此通用功能,并允许所有其他模型访问它。

Some questions before I start doing this (and maybe some general advice will also be great): 在开始执行此操作之前,需要先提出一些问题(也许一些通用建议也将是不错的选择):

1 - How should I go about it? 1-我应该怎么做? I was thinking creating a module in "lib" directory (is it the same as mixin class?) and then moving reusable view code to common partials. 我当时正在考虑在“ lib”目录中创建一个模块(与mixin类相同吗?),然后将可重用的视图代码移至公共部分。 What about the controller code? 控制器代码呢?

2 - As I was just learning Ruby on Rails during the coding of the first model, I went with a probably incorrect way of adding a bunch of methods to the controller. 2-当我在第一个模型的编码过程中刚刚学习Ruby on Rails时,我采用了一种可能错误的方法,即向控制器添加一堆方法。 I have a method that adds a comment (addcomment), adds a vote (addvote), etc. All these methods require non-standard (non-RESTful) routing via :collection. 我有一个添加评论(addcomment),添加投票(addvote)等的方法。所有这些方法都需要通过:collection进行非标准(非RESTful)路由。 From what I understand, the correct way would be to move comments controller functionality to its own controller and access via standard RESTful routes. 据我了解,正确的方法是将注释控制器功能移至其自己的控制器并通过标准RESTful路由进行访问。 Is this what I should be doing? 这是我应该做的吗?

3 - Many plugins (eg. act_as_commentable) do not explicitly require loading a Module, just a line "act_as_commentable" somewhere in the Model. 3-许多插件(例如act_as_commentable)不需要显式加载模块,而只需在模型中的某处插入“ act_as_commentable”行即可。 Can I use something like this for my common functionality? 我可以使用类似的功能来实现我的常用功能吗? How does it work? 它是如何工作的?

  1. A simple way is to split the code into modules and use mixin. 一种简单的方法是将代码拆分为模块并使用mixin。 A better way is to write your own plugins for your common code.. like act_as_commentable you can learn about it here: http://guides.rubyonrails.org/plugins.html 更好的方法是为自己的通用代码编写自己的插件。像act_as_commentable一样,您可以在此处进行了解: http : act_as_commentable

  2. The correct way is to do a comments controller, and have it nested to your models, giving a restful routes like this: /mymodelname/1/comments. 正确的方法是做一个注释控制器,并将其嵌套到您的模型中,从而提供一条宁静的路线,例如:/ mymodelname / 1 / comments。 An easy way to make such controllers is by using inherited_resources plugin . 制作此类控制器的一种简单方法是使用Inherited_resources插件 scroll down to the "Polymorphic belongs to" section- there is a comments controller example 向下滚动到“多态所属”部分,这里有一个注释控制器示例

For repeated model code, put it in a module in the lib directory. 对于重复的模型代码,请将其放在lib目录中的模块中。

For controller code, put your duplicate code in ApplicationController. 对于控制器代码,将重复的代码放入ApplicationController中。

For your view code, use partials. 对于您的视图代码,请使用partials。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM