Rails:基于角色的用户的 DRY 视图

发布于 2024-10-07 16:12:14 字数 390 浏览 2 评论 0原文

我很好奇现在 DRY 视图的最新技术是什么。我有一个已经发展了三年半的应用程序,而现在视图层并不是一个令人愉快的地方。

首先介绍一下背景,我有一个由多个不同用户组使用的应用程序,我需要添加更多内容。不同用户看到的内容之间存在大量重叠,但仍然有很多内容是每个组独有的。用户看到的细节内容和数量受到限制。这是基于标准角色的 Web 应用程序。

目前,该应用程序正在使用丑陋的混杂的部分(带有局部变量),以及使用部分、content_for 块和助手的部分,以及一大堆重复。

我正在寻找的是,人们现在在做什么来保持他们的观点干燥?

我开始研究 Cells (http://cells.rubyforge.org/),但我想知道还有什么可以解决这个问题。

您使用什么来实现复杂且基于角色的视图?

I'm curious what the state of the art is for DRY views these days. I've got an app that's been evolving for over three and a half years, and these days the view layer is not a happy place.

First a little background, I've got an app that is used by several different groups of users, and I need to add more. There is a significant amount of overlap between what different users see, but there's still a lot that is exclusive to each group. Users have restrictions what and how much detail they see. It's your standard role based web app.

Currently the app is using an ugly mishmash of partials (with local variables), and partials using partials, content_for blocks and helpers, and a whole bunch of duplication.

What I'm looking for is, what are people doing these days to keep their views DRY?

I started looking at Cells (http://cells.rubyforge.org/), but I wondered, what else is out there for solving this problem.

What are you using for complex and role-based views?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟若柳尘 2024-10-14 16:12:14

我面临着同样的问题/挑战。我已经开始实现 Cell,但遇到了困难,“content_for”不受您期望的方式支持,并且无法使用单元视图将内容添加到主布局。

这是 Rails 中的一个限制/Cell 使用 Rails 的方式,而且看起来并不是真正可以规避的。

如果您可以忍受这个限制,我认为这是重复内容块的最佳解决方案。

对我来说,上述问题的最佳解决方案如下:

如果整个单元格需要在不同的内容块中呈现,则可以使用以下内容来获得一个不错的API:

将以下代码段放入application_helper.rb中:

def render_cell_for(id, cell, action, options = nil, &block)
  content_for id do
    render_cell(cell, action, options, &block)
  end
end

在视图中使用

<%=render_cell_for(:content_for, :cell, :action) %>

以在块的内容中呈现。

I'm facing the same problem/challenge. I've started implementing Cell but ran into a wall, 'content_for' is not supported in the way you'd expect and it is not possible to add content to the master layout with it using a cell view.

This is a limitation in Rails / the way Cell uses rails and is not really circumventable it seems.

If you can live with this limitation I think it is the best solution for repeated content blocks.

Slightly offtopic the best solution to the above problem for me is the following:

If the entire cell needs to be rendered in a different content block the following can be used to get a nice api:

Put the following piece of code in the application_helper.rb:

def render_cell_for(id, cell, action, options = nil, &block)
  content_for id do
    render_cell(cell, action, options, &block)
  end
end

Use

<%=render_cell_for(:content_for, :cell, :action) %>

in your views to render within a content for block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文