我有一个有很多重复逻辑的观点。我不太清楚如何干它。
有什么想法吗?
<li><a href="#">Great Grandparents</a>
<ul>
<li><% if relative.humanize == "Great grandfather" || relative.humanize == "Great grandmother" %>
<%= link_to image_tag(membership.user.avatar.url, size: "48x48", :class => "img-circle") , family_tree_path(membership.user.family_tree) %>
<%= link_to membership.user.name, family_tree_path(membership.user.family_tree)%>
<% else %>
None added yet, add them <%= link_to 'here', "#" , class: 'btn invite popupbox','data-popup' => 'invite_friend' %>
<% end %>
</li>
</ul>
</li>
<li><a href="#">Grandparents</a>
<ul>
<li><% if relative.humanize == "Grandfather" || relative.humanize == "Grandmother" %>
<%= link_to image_tag(membership.user.avatar.url, size: "48x48", :class => "img-circle") , family_tree_path(membership.user.family_tree) %>
<%= link_to membership.user.name, family_tree_path(membership.user.family_tree)%>
<% else %>
None added yet, add them <%= link_to 'here', "#" , class: 'btn invite popupbox','data-popup' => 'invite_friend' %>
<% end %>
</li>
</ul>
</li>
<li><a href="#">Parents</a>
<ul>
<li><% if relative.humanize == "Mom" || relative.humanize == "Dad" %>
<%= link_to image_tag(membership.user.avatar.url, size: "48x48", :class => "img-circle") , family_tree_path(membership.user.family_tree) %>
<%= link_to membership.user.name, family_tree_path(membership.user.family_tree)%>
<% else %>
None added yet, add them <%= link_to 'here', "#" , class: 'btn invite popupbox','data-popup' => 'invite_friend' %>
<% end %>
</li>
</ul>
</li>
注意:上面已被截断,至少还有7个。有很多重复,但我不太确定如何以Railsy方式干它。
答案 0 :(得分:2)
在您的视图中执行:
<% relatives_sections = [
{ section_name: 'Great grandparents', human_names: ['Great grandfather', 'Great grandmother']},
{ section_name: 'Grandparents', human_names: ['Grandfather', 'Grandmother']},
{ section_name: 'Parents', human_names: ['Mom', 'Dad']},
...
] %>
<% relatives_sections.each do |section| %>
<li><a href="#"><%= section[:section_name] %></a>
<ul>
<li>
<% if section[:human_names].include?(relative.humanize) %>
<%= link_to image_tag(membership.user.avatar.url, size: "48x48", :class => "img-circle") , family_tree_path(membership.user.family_tree) %>
<%= link_to membership.user.name, family_tree_path(membership.user.family_tree)%>
<% else %>
None added yet, add them <%= link_to 'here', "#" , class: 'btn invite popupbox','data-popup' => 'invite_friend' %>
<% end %>
</li>
</ul>
</li>
<% end %>
您可能还想将整个<li>
块移动到包含
_relatives.html.erb
部分
<li><a href="#"><%= section_name %></a>
<ul>
<li>
<% if human_names.include?(relative.humanize) %>
<%= link_to image_tag(user.avatar.url, size: "48x48", :class => "img-circle") , family_tree_path(user.family_tree) %>
<%= link_to user.name, family_tree_path(user.family_tree)%>
<% else %>
None added yet, add them <%= link_to 'here', "#" , class: 'btn invite popupbox','data-popup' => 'invite_friend' %>
<% end %>
</li>
</ul>
</li>
然后,在您看来:
<% common_vars = {user: membership.user, relative:relative} %>
<% relatives_sections.each do |section| %>
<%= render partial: 'relatives', locals: section.merge(common_vars) %>
<% end %>
作为最后一步,您可以考虑将relative_sections
移动到帮助程序中,并在可能的情况下进一步将其干掉。