我有一个带有3个对象的控制器,我想用每个对象调用相同的部分三次
class HomeController < ApplicationController
def index
@evaluations_teams = Evaluation.eager_load(:user).where(users: {status: true}).where(team_id: current_user.student_details.last.team_id).all.order(created_at: :desc).limit 5
@evaluations_stage = Evaluation.eager_load(:user).where(users: {status: true}).where(stage_id: current_user.student_details.last.stage_id).all.order(created_at: :desc).limit 5
@evaluations_schools = Evaluation.eager_load(:user).where(users: {status: true}).where(school_id: current_user.student_details.last.school_id).all.order(created_at: :desc).limit 5
end
end
INDEX.HTML.ERB
<%= render 'home/partials/loop_areas', locals: {evaluations:@evaluations_teams} %>
<%= render 'home/partials/loop_areas', locals: {evaluations:@evaluations_stage} %>
<%= render 'home/partials/loop_areas', locals: {evaluations:@evaluations_schools} %>
部分:home / partials / _loop_areas.html.erb
<% @evaluations.each do |evaluation| %>
<div class="div-card-infosaluno">
<div class="card-aluno"><%= evaluation.user.full_name %></div>
<div class="card-serie"><%= "#{evaluation.stage.name} · #{evaluation.school.name}" %></div>
</div>
<% end %>
返回:
未定义的方法`每个&#39;为零:NilClass
on&lt;%@ evaluations.each do | evaluation | %GT;
我该怎么做?
答案 0 :(得分:2)
您尝试访问部分中的实例变量@evaluations
,但未在控制器操作中定义。
您需要循环遍历局部变量evaluations
。
<%# home/partials/_loop_areas.html.erb %>
<% evaluations.each do |evaluation| %>
...
<% end %>
你的观点应该是
<%= render 'home/partials/loop_areas', evaluations: @evaluations_teams %>
<%= render 'home/partials/loop_areas', evaluations: @evaluations_stage %>
<%= render 'home/partials/loop_areas', evaluations: @evaluations_schools %>
您也可以将散列传递给render
方法,但我觉得它更冗长。
<%= render partial: 'home/partials/loop_areas', locals: { evaluations: @evaluations_schools } %>
答案 1 :(得分:-1)
请在Partial中尝试以下代码:home / partials / _loop_areas.html.erb
它应该按照您的预期工作。
<% evaluations = local_assigns[:evaluations] %>
<% evaluations.each do |evaluation| %>
<div class="div-card-infosaluno">
<div class="card-aluno"><%= evaluation.user.full_name %></div>
<div class="card-serie"><%= "#{evaluation.stage.name} · #{evaluation.school.name}" %></div>
</div>
<% end %>
感谢。