Rails-抽象/共享视图 [英] Rails - Abstract/Shared Views

查看:68
本文介绍了Rails-抽象/共享视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些非常相似的对象.实际上,他们使用STI并将所有数据存储在DB的同一表中.当前,每次添加功能或修复错误时,都必须在3个不同的位置进行更新,因此我想对代码进行一些干燥.最常见的代码重复出现在视图中.我见过人们使用render :template => 'shared/something'呈现其共同的观点,问题是,我有许多观点,但仅针对一个特定的对象(装运),因此我更喜欢render :template => 'shipments/shared/something'render :template => 'abstract_shipments/something'之类的东西.不过,更重要的是,我希望任何实际的类都能够在需要时覆盖该模板.

I have a few objects that are incredibly similar. In fact, they use STI and store all of the data in the same table in the DB. Currently, every time I add a feature or fix a bug, I have to update it in 3 different places, so I would like to DRY my code a little bit. The most common code duplication is in the views. I have seen people using render :template => 'shared/something' to render their common views, the problem is, I have many of these, but only for one particular object (Shipments), so I would prefer something like render :template => 'shipments/shared/something' or render :template => 'abstract_shipments/something'. More importantly though, I would like any of the actual classes to be able to override the template if it needs to.

你们对此有什么建议吗?非常感谢您提供任何答案!

Do y'all have any suggestions on how to go about this? Thanks very much for any answers!

推荐答案

inherit_views插件可以完成此任务: http://github.com/ianwhite/inherit_views/tree/master .我以前已经成功使用过.

The inherit_views plugin might do the job: http://github.com/ianwhite/inherit_views/tree/master. I've used it successfully before.

然后,您可以为需要提货的所有控制器提供一个通用的基本控制器类,并在该基本控制器的views文件夹中具有任何通用模板,并在各个控制器的views文件夹中具有任何特定的替代.

You could then have a common base controller class for all controllers needing to render shipments and have any common templates in that base controller's views folder with any specific overrides in the individual controllers' views folders.

这篇关于Rails-抽象/共享视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆