Ruby on Rails中一个共享视图的两个控制器 [英] Two controllers for one shared view in Ruby on Rails

查看:88
本文介绍了Ruby on Rails中一个共享视图的两个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个用于两个相应模型的控制器,例如照片和类别.每个控制器中的index和show方法非常相似,并且视图相同.两种模型共享视图的最佳方法是什么?

我有两个选择:

  • 使用助手.在帮助器中将放置该视图的代码,并将从每个视图(照片/视图以及类别/视图)调用该帮助器.

  • 在每个视图中使用局部视图.我认为这是一种更干净的解决方案,但是在编写此解决方案的代码时,我会想到巨大的DRY.

因此,我有两个模型的两个控制器,每个控制器位于,并公开一个@photo对象(包含所有照片的photos控制器,以及仅包含所选类别照片的category控制器),并且我需要一个视图来显示这两个视图. /p>

我正在为此寻求一个优雅的解决方案,抱怨REST和DRY原理.有什么主意吗?

谢谢.

解决方案

我的一个项目也遇到类似情况.大多数控制器的所有删除视图的样式都相同,显示相同的确认框,并且可以对要删除的任何对象进行可预测的显示.

我认为该解决方案非常简单而优雅.简而言之,我们(开发人员)所做的就是在app/views中创建一个名为shared的新目录,并在其中放置共享视图.这些可能是完整的模板文件,也可能只是部分模板文件.

我建议您使用共享模板(既不在类别中也不在照片视图目录中,而是在共享目录中)并从视图中手动呈现它.

例如在控制器和文件app/views/shared/photo.html.erb中都有这样的方法:

def show
  @photo = Photo.first # ... or whatever here
  render :template => 'shared/photo'
end

这应该成功呈现共享模板.这是最干燥的路线,并且在每个控制器的视图目录中使用或多或少的空视图时仅包含一个共享的局部视图,就不会有污染的感觉,

I have two controllers for two respective models, by example, photos and categories. index and show methods are very similar in each controller, and the views are identical. What is the best method for share the view by the two models?

I've though two options:

  • Use a helper. In the helper will put the code for the view, and will call the helper from each view (photos/views and categories/views)

  • Use a partial in each views. I think it's a more clean solution, but I see huge DRY's in my mind when going to code this solution.

So, I have two controllers from two models, each one at and exposes a @photo object (photos controller with all the photos, and categories controller with just the selected categorie's photos) and I need one view to show both.

I'm looking for an elegant solution for this, complaining REST and DRY principes. Any idea?

Thanks in advance.

解决方案

I have a similar situation with one of my projects. All the delete views for most controllers are styled the same way, display the same confirmation boxes, and simply renders a predictable display of whatever object is being deleted.

The solution was quite simple and elegant in my opinion. Simply put, what we (the developers) did was create a new directory in app/views called shared and put shared views in there. These could be full template files or just partials.

I would suggest using a shared template (in neither categories nor photos view directories, but rather in the shared directory) and rendering it manually from the view.

e.g. have a method as such in both controllers and a file app/views/shared/photo.html.erb:

def show
  @photo = Photo.first # ... or whatever here
  render :template => 'shared/photo'
end

This should successfully render the shared template. It is the DRYest route and doesn't have the feeling of pollution you get when using a more-or-less empty view in each controller's view directory just to include a shared partial, as I understand your question is suggesting.

这篇关于Ruby on Rails中一个共享视图的两个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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