显示关联记录索引视图

时间:2018-08-18 01:54:02

标签: ruby-on-rails ruby postgresql ruby-on-rails-5

编辑#1

course_modules_controller.rb

def index
  @course_modules = CourseModule.all
  @courses = Course.all
  @course_exercises = CourseExercise.all
end

course_modules/index.html.erb

<section class="pt-4 px-8">
  <section class="flex flex-wrap justify-between">
    <h3 class="font-normal text-grey-dark mb-4 py-2">
      Course Modules
    </h3>
    <%= render 'layouts/dashboard/account' %>
  </section>
</section>

<section class="accordion-toggle">
  <section class="px-8">
    <% @courses.each do |course| %>
    <section class="w-full py-4">
      <section class="rounded shadow bg-grey-lighter">
          <section class="flex justify-between p-6 p-4">
            <section class="flex items-center px-6 text-grey-darker">
              <section class="font-bold text-base">
                <%= course.title %>
              </section>
            </section>

            <i class="flex items-center fal fa-angle-up ml-2"></i>
          </section>
        </section>
      </section>

      <section class="accordion-items hidden">
        <% @course_modules.each do |course_module| %>
          <section class="w-full">
            <section class="rounded shadow bg-grey-lighter border-b">
              <section class="flex justify-between px-6 p-4">
                <section class="flex items-center px-6 text-grey-darker">
                  <section class="font-bold text-base">
                    <%= course_module.title %>
                  </section>
                </section>

                <section class="modules">
                  <%= link_to "Edit", edit_course_module_path(course_module), class: "inline-block text-base text-grey-dark hover:text-darker px-4 py-2 border-2 border-grey leading-none no-underline hover:border-2 hover:border-grey-dark" %>
                </section>
              </section>
            </section>

            <section class="accordion-exercises">
              <% @course_exercises.each do |course_exercise| %>
                <section class="w-full">
                  <section class="rounded shadow bg-grey-lighter border-b">
                    <section class="flex justify-between px-6 p-4">
                      <section class="flex items-center px-6 text-grey-darker">
                        <section class="font-bold text-base pl-4">
                          - <%= course_exercise.title %>
                        </section>
                      </section>

                      <section class="exercises">
                        <%= link_to 'Edit', edit_course_exercise_path(course_exercise), class: "inline-block text-base text-grey-dark hover:text-darker px-4 py-2 border-2 border-grey leading-none no-underline hover:border-2 hover:border-grey-dark" %>
                      </section>
                    </section>
                  </section>
                </section>
              <% end %>
            </section>
          </section>
        <% end %>
      </section>
    <% end %>
  </section>
</section>

原始问题

我有三个模型,课程,课程模块和课程练习。

在课程模块的索引视图上,我想像这样显示课程,模块和练习

image

但是,似乎并没有考虑到ID

image

测试模块属于课程#1,模块#1属于课程#2,测试练习也属于模块#1

image

我该如何使用它以及要使用的ID。

即我只希望模块属于列出的课程,对于属于该模块的练习也可以这样说。

2 个答案:

答案 0 :(得分:0)

试图弄清楚您遇到的问题,这应该是一个简化的视图:

<% @courses.each do |course| %>
  <h3><%= course.title %></h3>
  <% course.course_modules.each do |course_module| %>
    <h4><%= course_module.title %></h4>
    <% course_module.course_exercises.each do |exercise| %>
      <p><%= exercise.title %></p>
    <% end %>
  <% end %>
<% end %>

渲染为:

<h3>Course #1</h3>
<h4>module #1-1</h4>
<p>#1-1-1</p>
<p>#1-1-2</p>
<p>#1-1-3</p>
<h4>module #1-2</h4>
<p>#1-2-1</p>
<p>#1-2-2</p>
<p>#1-2-3</p>
<p>#1-2-4</p>
<h3>Course #2</h3>
<h4>module #2-1</h4>
<p>#2-1-1</p>
<p>#2-1-2</p>
<p>#2-1-3</p>
<h4>module #2-2</h4>
<p>#2-2-1</p>
<p>#2-2-2</p>
<p>#2-2-3</p>

假设您的模型是

class Course < ApplicationRecord
  has_many :course_modules
end

class CourseModule < ApplicationRecord
  belongs_to :course
  has_many :course_exercises
end

class CourseExercise < ApplicationRecord
  belongs_to :course_module
end

答案 1 :(得分:0)

您遇到的问题非常简单。您遍历每个课程模块的每个课程练习,并遍历每个课程的每个课程模块。您应该在其中迭代属于特定课程模块的课程练习,并迭代属于特定课程的课程模块。

这意味着您的控制器应如下所示:

def index
  # the includes solves the N+1 query problem
  @courses = Course.all.includes(course_modules: :course_exercises)
end

您的视图应如下所示:

<% @courses.each do |course| %>
  <% # code for courses %>

  <% # instead of @course_modules.each %>
  <% course.course_modules.each do |course_module| %>
    <% # code for course modules %>

    <% # instead of @course_exercises.each %>
    <% course_module.course_exercises.each do |course_exercise| %>
      <% # code for course exercises %>
    <% end %>
  <% end %>
<% end %>

参考文献:

  • #course_modules#course_exercises辅助方法是通过指定has_many关联来创建的(请参见有关生成方法的链接)。
  • #includes解决N + 1查询问题。