使用部分和集合重构

时间:2012-01-01 13:29:10

标签: ruby-on-rails ruby-on-rails-3 views partial-views partials

可以通过使用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 %>

1 个答案:

答案 0 :(得分:4)

利用rendering collections

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

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