Rails 4 partial locals undefined variable 错误
Rails 4 partial locals undefined variable error
我正在渲染一堆带有图表的表格,所有相同类型的数据都重复了很多次,所以我想将部分数据用于 DRY 代码目的。
这是我在视图中调用的代码:
<%= render 'app_usage', locals: {
metric: "Sessions",
new_total: @flurry_total,
old_total: @flurry_old_total,
growth: flurry_growth,
chart_data: flurry_chart
} %>
并在 _app_usage.html.erb
<div class="col-sm-12">
<table class="table table-striped table-condensed">
<tbody>
<tr><td>Metric:</td><td><%= metric %></td></tr>
...
但是我得到一个关于 <tr><td>Metric:</td><td><%= metric %></td></tr>
的错误,说变量 metric
未定义。这是为什么?
您需要使用 partial
语法来调用 the ActionView::PartialRenderer
with locals
:
<%= render partial: 'app_usage', locals: {
metric: "Sessions",
new_total: @flurry_total,
old_total: @flurry_old_total,
growth: flurry_growth,
chart_data: flurry_chart
} %>
我正在渲染一堆带有图表的表格,所有相同类型的数据都重复了很多次,所以我想将部分数据用于 DRY 代码目的。
这是我在视图中调用的代码:
<%= render 'app_usage', locals: {
metric: "Sessions",
new_total: @flurry_total,
old_total: @flurry_old_total,
growth: flurry_growth,
chart_data: flurry_chart
} %>
并在 _app_usage.html.erb
<div class="col-sm-12">
<table class="table table-striped table-condensed">
<tbody>
<tr><td>Metric:</td><td><%= metric %></td></tr>
...
但是我得到一个关于 <tr><td>Metric:</td><td><%= metric %></td></tr>
的错误,说变量 metric
未定义。这是为什么?
您需要使用 partial
语法来调用 the ActionView::PartialRenderer
with locals
:
<%= render partial: 'app_usage', locals: {
metric: "Sessions",
new_total: @flurry_total,
old_total: @flurry_old_total,
growth: flurry_growth,
chart_data: flurry_chart
} %>