2017-05-30 178 views
0

我有有一些下面的代碼Rails應用程序減少代碼冗餘

  <ul class="sortable grid row"> 
      <% @videouploads.each do |video_upload| %> 
       <% if video_upload.category == 'モノナビ' && video_upload.priority == 1 %> 
        <%= render partial: 'adminrow', :locals => {:video_upload => video_upload } %> 
       <% end %> 
      <% end %> 
      </ul> 

我必須寫這個代碼10次video_upload.priority == 1其中從1到10的優先級變化如何避免代碼重複還是我有什麼是最好的解決方案?

+0

做一個循環? – Pete

回答

0
<% (1..10).each do |inc| %>  
    <ul class="sortable grid row"> 
    <% @videouploads.each do |video_upload| %> 
     <% if video_upload.category == 'モノナビ' && video_upload.priority == inc %> 
     <%= render partial: 'adminrow', :locals => {:video_upload => video_upload } %> 
     <% end %> 
    <% end %> 
    </ul> 
<% end %> 

這個怎麼樣?

0

環比優先

<% priorities = (1..10).to_a %> 
<% priorities.each do |priority| %> 
    <ul class="sortable grid row"> 
    <% @videouploads.each do |video_upload| %> 
     <% if video_upload.category == 'モノナビ' && video_upload.priority == priority %> 
     <%= render partial: 'adminrow', :locals => {:video_upload => video_upload } %> 
     <% end %> 
    <% end %> 
    </ul> 
<% end %>