刘华佼的全栈学习记录

8-15 partial的用法

| Comments

  <% @articles.each do |article| %>
    <tr>
      <td>#</td>
      <td><%= link_to(article.title, article_path(article)) %></td>
      <td><%= render_article_description(article) %></td>
      <td><%= article.user.email %></td>
      <td>
        <% if current_user && current_user == article.user %>
          <%= link_to("Edit", edit_article_path(article), class: "btn btn-sm btn-default")%>
          <%= link_to("Delete", article_path(article),    class: "btn btn-sm btn-default",
                      method: :delete, data: { confirm: "真的要删除吗?" } )%>
        <% end %>
      </td>
    </tr>
  <% end %>

这部分内容删除,改为:

  <%= render :partial => "article_item", :collection => @articles, :as => :article %>

然后在同一文件目录下新建_article_item.html, 内容与删掉的一样,仅仅少了第一句和最后一句:

  <% @articles.each do |article| %>
  <% end %>

我的猜想是:
render :partial => "article_item" 代表要调用文件_article_item.html
:collection => @articles, :as => :article有循环的意思,功能等同于:

  <% @articles.each do |article| %>
  <% end %>

Comments

comments powered by Disqus