刘华佼的全栈学习记录

如何使用进阶partial

| Comments

目标

使用进阶partial

步骤一:

app/views/groups/index.html.erb中的部分代码

 <% @groups.each do |group| %>
   <tr>
     <td>#</td>
     <td><%= link_to(group.title, group_path(group)) %></td>
     <td><%= render_group_description(group) %></td>
     <td> <%= group.user.email %> </td>
     <td>
       <% if current_user && current_user == group.user %>
         <%= link_to("Edit", edit_group_path(group), class: "btn btnsm btndefault")%>
         <%= link_to("Delete", group_path(group),    class: "btn btnsm btndefault",
                     method: :delete, data: { confirm: "Are you sure?" } )%>
       <% end %>
     </td>
   </tr>
 <% end %>

改为:
<%= render :partial => "group_item", :collection => @groups, :as => :group %>

步骤二:

新建app/views/groups/_group_item.html.erb,把上面被替换掉的那部分代码移到这个文件里。第一句和最后一句去掉。

Comments

comments powered by Disqus