Reputation: 1099
I'm rendering a partial from an offer's view:
<%= render partial: 'parent_offers_table', locals: { :parent_offers => [offer.parent_offer] } %>
And then, in _parent_offers_table:
<%= render partial: 'parent_offer', collection: parent_offers %>
However, in _parent_offer I cannot do anything with the parent_offer variable. I get undefined methods for nil:NilClass:
<tr>
<td><%= parent_offer.created_at %></td>
<td><%= parent_offer.version %></td>
<td><%= parent_offer.client.try(:name) %></td>
<td>
............
What am I doing wrong? Thanks!
Upvotes: 0
Views: 1021
Reputation: 76774
Collection
<%= render partial: "parent_offer", collection: parent_offers, as: :parent_offer %>
Although the collection
partial rendering functionality is truly amazing, we found it has one major drawback, in that it's very difficult to manage the object
you call in the partial itself.
If you're calling your own object name, you'll be best to use the as:
argument to define the local variable:
To use a custom local variable name within the partial, specify the :as option in the call to the partial:
<%= render partial: "product", collection: @products, as: :item %>
With this change, you can access an instance of the @products collection as the item local variable within the partial.
Upvotes: 1
Reputation: 2165
instead:
<%= render partial: 'parent_offer', collection: parent_offers %>
try:
<%= render partial: 'parent_offer', locals: {parent_offers: parent_offers} %>
or:
<%= render 'parent_offer', parent_offers: parent_offers %>
Upvotes: 1