【问题标题】:Only show records belonging to parent仅显示属于父级的记录
【发布时间】:2018-08-18 08:32:29
【问题描述】:

所以我正在开发一个应用程序,但是,当我去显示模块记录时,它们都会显示,包括不应该显示的记录。

上下文

我有三个模型,coursecourse_modulescourse_exercises。我试图在课程显示页面上显示属于该课程的模块,但我只想显示属于该课程的course_modules

show.html.erb

<section class="flex h-64 hero-banner p-4">
  <section class="flex items-center mx-auto">
    <h2 class="uppercase">
      <%= @course.title %>
    </h2>
  </section>
</section>

<section class="pt-4 px-4">
  <section class="w-full">
    <section class="rounded overflow-hidden shadow">
      <section style="padding: 56.25% 0 0 0; position: relative;">
        <iframe src="https://player.vimeo.com/video/<%= @course.trailer %>" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
      </section>
    </section>
  </section>

  <section class="flex flex-wrap -mx-4">
    <section class="w-full lg:w-3/4 p-4">
      <section class="bg-grey-lightest shadow text-grey-darker p-4">
        <h2 class="font-normal mb-4">Course description</h2>
        <p class="font-normal whitespace-pre-wrap"><%= @course.description %></p>
      </section>
    </section>
    <section class="w-full lg:w-1/4 p-4">
      <section class="bg-grey-lightest shadow text-grey-darker p-4 mb-4">
        <h3 class="font-normal mb-4">Course Price</h3>
        <p class="font-bold text-3xl text-green">£<%= @course.price %></p>
      </section>

      <%= button_to "Buy now", "#", class: "bg-blue hover:bg-blue-dark w-full text-white font-semibold py-3 px-4 border-2 rounded-sm border-blue-dark shadow outline-none" %>

      <section class="bg-grey-lightest shadow text-grey-darker py-4 px-4 mt-4">
        <h3 class="font-normal mb-4">Course Modules</h3>
          <% @course_modules.each do |course_module| %>
            <section class="py-2 border-b-2 border-light modules">
              <%= course_module.title %>
            </section>
        <% end %>
      </section>
    </section>
  </section>
</section>

courses_controller

def show
  @course_modules = CourseModule.all
end

course.rb

class Course < ApplicationRecord
  has_many :course_modules

  validates :title, :summary, :description, :trailer, :price, presence: true
end

course_module.rb

class CourseModule < ApplicationRecord
  belongs_to :course
end

schema.rb

create_table "course_modules", force: :cascade do |t|
    t.string "title"
    t.integer "course_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["course_id"], name: "index_course_modules_on_course_id"
  end

  create_table "courses", force: :cascade do |t|
    t.string "title"
    t.text "summary"
    t.text "description"
    t.string "trailer"
    t.integer "price"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

这是数据库的截图

测试模块属于第一门课程,模块#1属于第二门课程,但是,两个模块都显示了

感谢任何帮助。

【问题讨论】:

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


    【解决方案1】:

    好吧,如果这是一个表演动作,我想它应该只显示一个课程。我们需要一个 id。所以

    def show
      course = Course.find(params[:id])
      @course_modules = course.course_modules
    end
    

    我让它变得非常简单,并假设您肯定会找到该课程。

    【讨论】:

    • 啊,太好了,这种方法对任何操作、索引等是否都一样有效?
    • 视情况而定。我的意思是,例如,默认情况下,在 index 操作中,您通常不需要 id,因为您显示了该模型的所有实体。有意义吗?
    • 是的,假设我想在课程模块索引中显示课程模块和练习,就像gyazo.com/a2cbe309db8ca649163f5264da3b44fa 我将如何在索引中显示两者? gyazo.com/75c1c5b28c2debaeb4547650401a5abf
    • 在您的控制器索引操作中,只需加载所有课程模块@modules=CourseModule.all 并在您的索引视图中迭代每个模块@modules.each do |m|,然后在循环内再次迭代所有练习m.exercises.all.each do |e|,前提是练习是一个孩子当然当然是模块。基本上,类的父类或子类都可以作为该类class.parentclass.child 的方法调用。 class 是您查询的模型对象
    猜你喜欢
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多