0
votes

From a design perspective, what advantages or necessities are associated with requiring passing variables to partial in order to access them from the partials?

What would be the problem or disadvantage of simply being able to use variables in the partial that were already set by the parent/grandparent views?

1

1 Answers

6
votes

The advantage of passing variables to a partial is that you can call methods on the objects passsed without needing to know the original variable name.

For example, if you had a partial to show error messages in ActiveRecord instances you could code a partial as...

<% if object.errors.any? %>
  <div id="error_explanation">
   <div class="alert alert-error">
     The form contains <%= pluralize(object.errors.count, "error") %>.
   </div>
   <ul>
    <% object.errors.full_messages.each do |msg| %>
    <li>* <%= msg %></li>
   <% end %>
   </ul>
 </div>
<% end %>

note the generic reference to object... I don't need to worry about whether the original object was a @post or @user or @account or @customer ... the partial works for any or all of those provided the original object is passed as object.