DRYing rails 视图:部分 vs 助手 [英] DRYing rails view: partial vs helper

查看:16
本文介绍了DRYing rails 视图:部分 vs 助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要关于 DRYing 视图代码的最佳实践的建议.我的应用程序中有三个类(NewsItem、RssItem 和 BlogItem),它们使用单​​独的视图,但其中有相似的部分.其中一部分是这样的:

I need an advice on best practices for DRYing view code. I have three classes (NewsItem, RssItem and BlogItem) in my app, that use separate views, but have similar parts in them. One of the parts is such:

<% if current_user %>
<footer>
  <%= marks_bar(@item) %>
  <%= favorite_button(@item, "blog_item") || delete_from_favorite_button(@item, "blog_item") %>
  <%= share_button(@item) %>
  <% if current_user.is_mine?(@item) %>
    <div><%= link_to "Edit", edit_user_blog_item_path(current_user, @item) %></div>
  <% end %>
</footer>
<% end %>

三个班级几乎相等,所以我决定把它带到不同的地方.在这里我很困惑:我应该使用部分方法还是辅助方法?我知道助手主要用于将 ruby​​ 代码与 HTML 分离,但在这种情况下,助手将如下所示:

It's almost equal for all three classes, so I decided to take it out to some separate place. And here I am confused: should I use a partial or helper method for it? I know that helpers are used mostly for separating ruby code from HTML, but in this case the helper will look like:

def toolbar_for(item, type_str, edit_path)
  if current_user
    content_tag(:footer) do |b|
      marks_bar(item).to_s <<
      (delete_from_favorite_button(item, type_str) || favorite_button(@item, type_str)).to_s <<
      share_button(@item).to_s <<
      (content_tag(:div) { link_to("Edit", edit_path)} if current_user.is_mine?(@item)).to_s
    end
  end
end

所以,这里几乎没有 HTML 代码.

So, there is almost no HTML code here.

能否请您给我建议,您认为哪种方法更好,为什么?此外,这些方法中是否存在一些性能问题(例如,多个字符串连接或频繁的部分加载可能代价高昂)?(这个应用加载量比较大)

Could you please give me advice, what method is better in your opinion and why? Also, are there some performance issues in these methods (for example multiple String concatenation or frequent partial loading might be costly)? (This app is rather high-loaded)

推荐答案

我想说这是部分的一个很好的例子.

I would say this is a great example of a partial.

我保留了生成任意动态内容的助手.这是一种松散的描述,那么举个例子吧:我会制作一个帮助器来拆分 ActiveRecord 对象的数组并将它们显示到 N 列中.在这种情况下,传递的对象的结构以某种方式被利用以生成内容,但内容本身并不重要.仔细想想,form_for 也符合这个描述.

I reserve helpers for generating arbitrary dynamic content. This is kind of a loose description so how about an example: I would make a helper that splits an array of ActiveRecord objects and displays them into N columns. In this case the passed object's structure is being leveraged in some way in order to generate the content, but the content itself is unimportant. If you think about it, form_for also fits this description.

相反,partials 非常适合需要在多个页面上重复使用的静态"内容.例如,您将创建一个部分来呈现单个项目.部分确定特定项目的静态"表示.

Conversely partials are great for 'static' content, that needs to be reused on multiple pages. For instance you would create a partial to render a single item. The partial determines a particular items 'static' representation.

在我看来,你的例子更适合第二个垃圾箱.由于 @item 没有被操纵来生成内容,实际上它几乎没有被使用.似乎这个助手主要是其他适当创建的助手(share_buttonmarks_bar)的胶水代码.部分的完美用例!

Your example fits much better into the second bin, in my opinion. Since the @item isn't being manipulated to generate the content, in fact it is hardly being used. It seems this helper is mostly glue code for other appropriately created helpers (share_button and marks_bar). The perfect use case for a partial!

这篇关于DRYing rails 视图:部分 vs 助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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