Reputation: 1017
I am trying to dry up code for a couple of reports.
I took the table header code and put that in a partial. That works fine when I call that with a render partial. However, when I try to move the code for the body of the table, I am getting a 'something went wrong' error.
My original code that works is
<% @complaints.each do |complaint| %>
<% if complaint.product_name == $complaint_group_name then %>
<tr>
<td><%= link_to complaint.id, complaint %></td>
<td style="text-align: right;"><%= complaint.length %></td>
<td style="text-align: center;"><%= complaint.disposition %></td>
<td width="20%"><%= complaint.sales_order %></td>
. . .
<% end %>
<% end %>
The code that is not working is when I've moved that to a partial and try to render a partial
<% @complaints.each do |complaint| %>
<% if complaint.product_name == $complaint_group_name then %>
<%= render :partial => 'report_body' %>
<% end %>
<% end %>
I see an error of
I, [2016-07-12T10:25:09.046754 #95324] INFO -- : Rendered complaints/report_by_product.html.erb within layouts/application (359.4ms)
I, [2016-07-12T10:25:09.046754 #95324] INFO -- : Completed 500 Internal Server Error in 375ms (ActiveRecord: 140.6ms)
F, [2016-07-12T10:25:09.046754 #95324] FATAL -- :
ActionView::Template::Error (undefined local variable or method `complaint' for #<#<Class:0x51caab8>:0x95242f8>):
1:
2:
3: <tr>
4: <td><%= link_to complaint.id, complaint %></td>
5: <td style="text-align: right;"><%= complaint.length %></td>
6: <td style="text-align: center;"><%= complaint.disposition %></td>
7: <td width="20%"><%= complaint.sales_order %></td>
app/views/complaints/_report_body.html.erb:4:in `_app_views_complaints__report_body_html_erb__209592603_78833796'
Apparently the |complaint| part of the block does not get recognized by the partial. I either need to be able to pass this or include it in the partial - but then it gets messy because the grouping for each report would need to be handled in the partial.
The short question is can I pass |complaint| to the partial?
Upvotes: 0
Views: 39
Reputation: 3126
Or you can use short form like this
<%= render 'report_body', complaint: complaint %>
which is equivalent to
<%= render partial: 'report_body', locals: { complaint: complaint } %>
Upvotes: 2
Reputation: 297
Try to pass 'complaint' as a local variable to partial:
render partial: 'report_body', locals: { complaint: complaint }
Upvotes: 1