ruby-on-rails - 只显示属于父母的记录
问题描述
所以我正在开发一个应用程序,但是,当我去显示模块记录时,它们都会显示,包括不应该显示的记录。
语境
我有三个模型course
,course_modules
和course_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 属于第二门课程,但是,无论如何,两个模块都显示
任何帮助表示赞赏。
解决方案
好吧,如果这是一个表演动作,我想它应该只显示一个课程。我们需要一个 id。所以
def show
course = Course.find(params[:id])
@course_modules = course.course_modules
end
我让它变得非常简单,并假设您肯定会找到该课程。
推荐阅读
- python - 如何使用爱普生打印条形码
- python - 同步方法之间的通信
- postgresql - PostgreSQL LISTEN/NOTIFY 具有相同有效负载的每个事务的通知数
- javascript - 在 ReactJS 的子组件中将变量传递给处理程序
- r - 从 R 将记录附加到 SQL Server 中的表
- intellij-idea - 如何在 PhpStorm for JSX 上拆分和加入标签?例如:“ ”到“ ”
- python - SciPy Griddata“得到参数‘方法’的多个值”错误
- google-analytics - 如何在 Google Analytics 上更改网站所有者?
- codeigniter - 记录了codeigniter错误:同时针对文件夹中的所有文件“语言文件不包含数据”
- html - css flexbox布局模块中的表格设计