简体   繁体   English

如何在我的Rails应用程序的CRUD部分上干?

[英]How to DRY on CRUD parts of my Rails app?

I am writing an app which - similarly to many apps out there - is 90% regular CRUD things and 10% "juice", where we need nasty business logic and more flexibility and customization. 我正在编写一个应用程序 - 与许多应用程序类似 - 有90%的常规CRUD和10%的“果汁”,我们需要讨厌的业务逻辑和更多的灵活性和自定义。

Regarding this 90%, I was trying to stick to the DRY principle as much as I can. 关于这90%,我尽可能地坚持DRY原则。 As long as controllers go, I have found resource_controller to really work, and I could get rid of all the controllers on that area, replacing them with a generic one. 只要控制器运行,我发现resource_controller真的有效,我可以摆脱该区域的所有控制器,用通用控制器替换它们。

Now I'd like to know how to get the same with the views. 现在我想知道如何使用相同的视图。 On this app I have an overall, application.html.erb layout and then I must have another layout layer, common for all CRUD views and finally a "core" part: 在这个应用程序上,我有一个整体的application.html.erb布局,然后我必须有另一个布局层,所有CRUD视图通用,最后是“核心”部分:

  • On index.html.erb all I need to generate a simple table with the fields and labels I indicate. 在index.html.erb上,我需要生成一个包含我指示的字段和标签的简单表。

  • For new and edit, also generic form edition, indicating labels and fields (with a possibility of providing custom fields if needed). 对于新的和编辑,也是通用表单版本,指示标签和字段(如果需要,可以提供自定义字段)。

  • I am not sure I will need show, but if I do it would be the same as new and edit. 我不确定我是否需要演出,但是如果我这样做,它将与新的和编辑相同。

What plugins and tools (or even articles and general pointer) would help me to get that done? 什么插件和工具(甚至文章和一般指针)可以帮助我完成这项工作?

Thanks, Felipe. 谢谢,菲利普。

Looks like there is just a new gem out that is very close to your requirements: 看起来只有一个非常接近您要求的新宝石:

http://github.com/codez/dry_crud http://github.com/codez/dry_crud

Based on a common superclass (CrudController), you may inherit CRUD functionality for your various model controllers and adapt what is special there. 基于一个常见的超类(CrudController),您可以为各种模型控制器继承CRUD功能并调整其中的特殊功能。 This is what you already did, most probably. 这是你已经做过的,最有可能的。

The new thing about dry_crud is that also views and partials are inheritable. 关于dry_crud的新事物是视图和部分也是可继承的。 You define a common base template for each CRUD action, maybe divided into a couple of partials. 您为每个CRUD操作定义一个公共基本模板,可能会分成几个部分。 Thanks to the provided helpers, forms and tables may be generically defined by looking at the column definitions of the current model. 由于提供的帮助程序,可以通过查看当前模型的列定义来一般性地定义表单和表。 In your specific model's views, you then may adapt only the partials or views that need customizing. 在特定模型的视图中,您可以仅调整需要自定义的部分或视图。

Have a look at the documentation found on the site above and stay DRY! 看看上面网站上的文档并保持干燥!

You could run script/generate scaffold test name:string description:text valid:boolean and look at the views that generates (and run script/destroy scaffold test to remove the files). 您可以运行script/generate scaffold test name:string description:text valid:boolean并查看生成的视图(并运行script/destroy scaffold test以删除文件)。 That will give you a good sense of the standard way to write the 4 default Rails views. 这将使您很好地了解编写4个默认Rails视图的标准方法。

I would also recommend reading the relevant chapters in "Agile Web Development with Rails" and "The Rails Way". 我还建议阅读“使用Rails进行敏捷Web开发”和“The Rails Way”中的相关章节。

If you have existing views that need to be cleaned up, this episode of Railscasts is great: Cleaning Up the View 如果你有现有的视图需要清理,这集Railscasts很棒: 清理视图

Dary D进制

If you have DRYed up the controllers and now wish to DRY the views, one approach is to render :action => *actionname* and storing the UI contents that may change into instance variables (So that they are available on the view) This way you would be able to re-use the same view of edit , new , list or show . 如果您已经干掉控制器并且现在希望干掉视图,一种方法是render :action => *actionname*并存储可能更改为实例变量的UI内容(以便它们在视图中可用)这种方式您将能够重复使用相同的编辑新建列表显示视图。 For example, You are editing something related to Foo then you title should read Editing <%= @type %> , so should your form helpers. 例如,您正在编辑与Foo相关的内容,那么您的标题应该为Editing <%= @type %> ,因此您的表单助手也应如此。 Foo could well then change to bar . Foo可能会改为bar Thus you are re-using the same view for different entities (or controllers should I say). 因此,您将为不同的实体(或控制器,我应该说)重新使用相同的视图。 Remember that, Unlike redirect_to , render :action only renders the view and does not call the controller action of the action it is trying to render. 请记住,与redirect_to不同, render :action仅呈现视图,并且不会调用它尝试呈现的操作的控制器操作。

One thing for sure, if you wish to DRY up anything, you need to standardize or follow a convention. 有一点可以肯定的是,如果你想干掉任何东西,你需要标准化或遵循惯例。 Example, the structure of your views, in this case. 例如,在这种情况下,您的视图的结构。

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

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