干燥导轨视图:部分vs帮助器 [英] DRYing rails view: partial vs helper

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

问题描述

我需要有关烘干视图代码的最佳做法的建议。
我的应用程序中有三个类(NewsItem,RssItem和BlogItem),它们使用单​​独的视图,但其中包含相似的部分。其中一部分是这样的:

 <%,如果current_user%> 
< footer>
<%= marks_bar(@item)%>
<%= favorite_button(@item, blog_item)|| delete_from_favorite_button(@item, blog_item)%>
<%= share_button(@item)%>
<%如果current_user.is_mine?(@ item)%>
< div><%=链接到编辑,edit_user_blog_item_path(current_user,@item)%>< / div>
<%end%>
< / footer>
<%end%>

这三个班几乎都相等,所以我决定将它带到另一个地方。在这里,我感到困惑:我应该使用部分方法还是辅助方法?
我知道辅助程序主要用于从HTML分离ruby代码,但是在这种情况下,辅助程序看起来像:

  deftoolbar_for(item,type_str,edit_path)
,如果current_user
content_tag(:footer)做| b |
mark_bar(item).to_s<<
(delete_from_favorite_button(item,type_str)||收藏夹按钮(@item,type_str))。to_s<<
share_button(@item).to_s<<
(content_tag(:div){link_to( Edit,edit_path)}如果current_user.is_mine?(@ item))。to_s
结束
结束
结束

因此,这里几乎没有HTML代码。



请您给我建议,您认为哪种方法更好,为什么?此外,这些方法是否存在一些性能问题(例如,多个String串联或频繁的部分加载可能会花费很大)? (此应用程序的负载很高)

解决方案

我会说这是一个很好的部分示例。



我保留了用于生成任意动态内容的助手。这是一个宽松的描述,下面是一个示例:我将创建一个帮助程序,将一个ActiveRecord对象数组拆分为N列。在这种情况下,可以某种方式利用传递的对象的结构来生成内容,但是内容本身并不重要。



反之,分页非常适合静态内容,需要在多个页面上重复使用。



例如,您将创建一个局部以渲染单个项目。局部变量确定特定项目的静态表示形式。



我认为您的示例更适合第二个分类。由于 @item 未被操纵来生成内容,因此实际上几乎没有使用它。似乎该帮助程序主要是其他适当创建的帮助程序( share_button marks_bar )的粘合代码。完美的部分用例!


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 %>

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

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.

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.

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.

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!

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

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