我已经阅读了很多指南,但我无法理解为什么我的方法不起作用:
expression2
" _training_project.html.erb"和" _training_course.html.erb"文件位于学生文件夹中。
更新
在上面的代码中,循环错误,永远不会进入。
module StudentsHelper
def get_training_projects
@training_projects_student = TrainingProject.where(student_id: @student.id)
@training_projects_student.count
end
def get_training_courses
@training_course_student = TrainingCourse.joins(:courses_students).where(courses_students: { student_id: @student.id })
@training_course_student.count
end
def storico_studente
n = get_training_courses + get_training_projects
i_projects = 0
i_courses = 0
until i_courses + i_projects < n
if @training_course_student[i_courses].data_inizio < @training_projects_student[i_projects].data_inizio
render partial: "training_course", collection:@training_course_student[i_cousers]
i_courses += 1
else
render partial: "training_project", collection: @training_course_student[i_projects]
i_projects += 1
end
end
end
end
现在循环工作正常,我可以在日志中看到正确的调用:
def storico_studente
n_training_courses = get_training_courses
n_training_projects = get_training_projects
n = n_training_courses + n_training_projects
i_projects = 0
i_courses = 0
while i_courses + i_projects < n
if @training_course_student[i_courses].nil?
render partial: 'students/training_project', collection: @training_course_student[i_projects]
i_projects += 1 if i_projects < n_training_projects
elsif @training_projects_student[i_projects].nil?
render partial: 'students/training_course', collection: @training_course_student[i_courses]
i_courses += 1 if i_courses < n_training_courses
elsif @training_course_student[i_courses].data_inizio < @training_projects_student[i_projects].data_inizio
render partial: 'students/training_course', collection: @training_course_student[i_courses]
i_courses += 1 if i_courses < n_training_courses
else
render partial: 'students/training_project', collection: @training_course_student[i_projects]
i_projects += 1 if i_projects < n_training_projects
end
end
end
文件仅包含(暂时)
Rendered students/_training_course.html.erb (0.0ms)
Rendered students/_training_project.html.erb (0.0ms)
Rendered students/_training_course.html.erb (0.0ms)
结束
<div>ptoject</div>
这解释了加载时间(0.0ms)
但它仍然无效!
答案 0 :(得分:1)
我认为您的until
循环应该是while
循环。由于i_projects
和i_courses
都已初始化为零且n
始终大于或等于零,因此您的循环仅在n
为零时执行。
<强>更新强>
问题是你的帮助方法没有返回渲染的部分。它正在渲染它们,正如您的日志所确认的那样,但结果永远不会被返回。我确信有更好的方法可以做到,但这是一个解决方案:
def storico_studente
n_training_courses = get_training_courses
n_training_projects = get_training_projects
n = n_training_courses + n_training_projects
i_projects = 0
i_courses = 0
partials = []
while i_courses + i_projects < n
if @training_course_student[i_courses].nil?
partials << render(partial: 'students/training_project', collection: @training_course_student[i_projects])
i_projects += 1 if i_projects < n_training_projects
elsif @training_projects_student[i_projects].nil?
partials << render(partial: 'students/training_course', collection: @training_course_student[i_courses])
i_courses += 1 if i_courses < n_training_courses
elsif @training_course_student[i_courses].data_inizio < @training_projects_student[i_projects].data_inizio
partials << render(partial: 'students/training_course', collection: @training_course_student[i_courses])
i_courses += 1 if i_courses < n_training_courses
else
partials << render(partial: 'students/training_project', collection: @training_course_student[i_projects])
i_projects += 1 if i_projects < n_training_projects
end
end
safe_join partials
end