2012-01-01 50 views
0

可以通過使用partials的集合參數來重構和簡化以下代碼?我看不出它如何能因爲在傳遞局部變量的「路徑」來完成使用部分和集合的重構

利用 rendering collections
<% @admins.each do |admin| %> 
    <%= render :partial => 'person', :locals => { :person => admin, :path => edit_admin_path(admin) } %> 
<% end if @admins %> 

<% @users.each do |user| %> 
    <%= render :partial => 'person', :locals => { :person => user, :path => edit_user_path(user) } %> 
<% end if @users %> 

回答

4

<%= render :partial => "person", :collection => @users + @admins %> 

# in person.html.erb 
path = person.admin? edit_admin_path(person) : edit_user_path(person)