2014-10-08 58 views
0

我有一個觀點,有很多重複的邏輯。我不太清楚如何烘乾它。如何幹這個視圖?

任何想法?

 <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

會員資格和親戚如何相互關聯? – Surya 2014-10-09 04:18:25

回答

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成爲助手,如果可能的話,在那裏進一步乾燥。