可以通过使用partials的collection参数来重构和简化以下代码吗?由于传递了“path”局部变量,我无法看到它是如何完成的。
<% @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 %>
答案 0 :(得分:4)
<%= render :partial => "person", :collection => @users + @admins %>
# in person.html.erb
path = person.admin? edit_admin_path(person) : edit_user_path(person)