gpt4 book ai didi

ruby-on-rails - Rails4 : How to display the value one time only

转载 作者:太空宇宙 更新时间:2023-11-03 18:11:49 25 4
gpt4 key购买 nike

我只想显示一次房间名称。总结如下;

文章表

id day start_time title
2 1 09:00 Math
4 2 10:00 English
5 1 11:00 Science
8 3 12:00 Physics
9 2 13:00 Music

房间表

id day name
3 1 classA
6 2 classB
7 3 classC

我想在 View 中显示如下数据;

Day1
09:00 Math
11:00 Science
Room classA

Day2
10:00 English
13:00 Music
Room classB

请告诉我如何显示上面的值。每次在下面的代码中显示房间名称。

查看代码

<div class="row">
<% @article.group_by(&:day).each do |day, articles| %>
<h3>Day <%= day %></h3>

<% articles.each do |a| %>
<%= a.start_time %>
<% a.category %>
<%= a.contents %><br>
<%= a.matching_detail.try(:name) %><br> #I'd like to display only one time
<% end %>

<% end %>
</div>

模型代码

class Article < ActiveRecord::Base
belongs_to :school
has_many :rooms, through: :school
default_scope -> { order(day: :asc, start_time: :asc) }

def matching_detail
rooms.find_by_day day
end
end

class Room < ActiveRecord::Base
belongs_to :school
belongs_to :article
end

架构

ActiveRecord::Schema.define(version: 20150999999999) do

create_table "rooms", force: true do |t|
t.integer "school_id"
t.integer "day"
t.string "name"
t.string "detail"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "articles", force: true do |t|
t.integer "school_id"
t.integer "day"
t.string "start_time"
t.string "end_time"
t.integer "category"
t.string "title"
t.string "contents"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "schools", force: true do |t|
t.string "title"
t.datetime "created_at"
t.datetime "updated_at"
end

end

最佳答案

我通过以下方式重新创建了您的 Rails 项目:

  1. rails new SchoolScheduler
  2. 从你的问题中添加了模式文件
  3. rails db:schema:load
  4. 添加了您的模型文件 - 文章和房间
  5. 在 db/seeds.rb 中添加您的测试数据以填充数据库(见下文)

然后我做了一些修改:

  1. 为学校添加了一个脚手架:rails g scaffold School title:string(必须删除添加到 db/migrate 的迁移文件)
  2. 更新了学校模型(见下文)
  3. 更新了学校 View (show.html.erb,见下文)

然后我测试了一下:

  1. 启动 rails s
  2. 导航到 http://localhost:3000/schools
  3. 点击“显示”以显示“您要显示的那个”
  4. 瞧!

Screenshot of final view

db/seeds.rb

School.delete_all

School.create(id: 1, title: "The One You Want To Show")
School.create(id: 2, title: "Some Other School")

Article.delete_all

Article.create(id: 2, school_id: 1, day: 1, start_time: "09:00", title: "Math")
Article.create(id: 4, school_id: 1, day: 2, start_time: "10:00", title: "English")
Article.create(id: 5, school_id: 1, day: 1, start_time: "11:00", title: "Science")
Article.create(id: 8, school_id: 2, day: 3, start_time: "12:00", title: "Physics")
Article.create(id: 9, school_id: 1, day: 2, start_time: "13:00", title: "Music")

Room.delete_all

Room.create(id: 3, school_id: 1, day: 1, name: "classA")
Room.create(id: 6, school_id: 1, day: 2, name: "classB")
Room.create(id: 7, school_id: 2, day: 3, name: "classC")

应用/模型/school.rb

class School < ActiveRecord::Base
has_many :rooms
has_many :articles

def days
self.articles.pluck(:day).uniq
end
end

app/views/school/show.html.erb

<p id="notice"><%= notice %></p>

<p>
<strong>Title:</strong>
<%= @school.title %>
</p>

<% @school.days.each do |d| %>

<h3>Day<%= d %></h3>

<% @school.articles.where(day: d).each do |a| %>
<%= a.start_time %>
<%= a.title %><br>
<% end %>

<p>Room <%= @school.rooms.where(day: d).first.name %></p>

<% end %>

</br>
<%= link_to 'Edit', edit_school_path(@school) %> |
<%= link_to 'Back', schools_path %>

如果我是你,我可能会将 View 中的 #where.(day: d)... 重构到模型中,但这是次要的。

事后,您可以将问题中的 Article 和 Room 模型简化为:

class Article < ActiveRecord::Base
belongs_to :school
end

class Room < ActiveRecord::Base
belongs_to :school
end

关于ruby-on-rails - Rails4 : How to display the value one time only,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32726789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com