Partial là fragment ERB tái sử dụng — file tên bắt đầu bằng _ (underscore).
erb
<%# app/views/posts/_post.html.erb %>
<article>
<h2><%= post.title %></h2>
<p><%= post.body %></p>
</article>erb
<%# app/views/posts/index.html.erb %>
<%= render @posts %> <%# Rails tự render _post.html.erb cho mỗi item %>
<%= render partial: "post", locals: { post: @featured_post } %>Khi truyền collection (render @posts), Rails tự lặp và dùng local variable khớp tên partial (post cho _post.html.erb) — không cần .each trong view.
A partial is a reusable ERB fragment — its filename starts with _ (underscore).
erb
<%# app/views/posts/_post.html.erb %>
<article>
<h2><%= post.title %></h2>
<p><%= post.body %></p>
</article>erb
<%# app/views/posts/index.html.erb %>
<%= render @posts %> <%# Rails auto-renders _post.html.erb for each item %>
<%= render partial: "post", locals: { post: @featured_post } %>When passing a collection (render @posts), Rails iterates automatically and uses a local variable matching the partial name (post for _post.html.erb) — no .each needed in the view.