gpt4 book ai didi

ruby-on-rails - 如何使用 lookup_context 使此 View 尽可能干燥?

转载 作者:行者123 更新时间:2023-12-04 07:37:43 27 4
gpt4 key购买 nike

首先,这是我试图复制的观点:

screenshot of grades layout

这是来自该布局的 HTML(无论如何,来自 SAT 部分,您可以推断其余部分):

<table class="table table-hover table-bordered">
<thead>
<td colspan="2" class="text-center">
<strong>SAT</strong>
</td>
<tr>
<th>Subject</th>
<th>Grade</th>
</tr>
</thead>
<tbody>
<tr>
<td>Reading</td>
<td>900</td>
</tr>
<tr>
<td>Math</td>
<td>700</td>
</tr>
<tr>
<td>Writing</td>
<td>800</td>
</tr>
<tr>
<td><strong>Total</strong></td>
<td><strong>2,400</strong></td>
</tr>
</tbody>

这是我的 Grade.rb模型看起来像:
# == Schema Information
#
# Table name: grades
#
# id :integer not null, primary key
# subject :string
# result :string
# grade_type :integer
# profile_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#

class Grade < ActiveRecord::Base
belongs_to :profile

enum grade_type: { csec: 0, cape: 1, sat: 2, g7: 3, g8: 4, g9: 5, g10: 6, g11: 7, g12: 8, g13: 9 }
end

这是该表当前的样子,即在使用 lookup_context 之前Rails 中的方法:
<table class="table table-hover table-bordered">
<thead>
<td colspan="2" class="text-center">
<strong>SAT</strong>
</td>
<tr>
<th>Subject</th>
<th>Grade</th>
</tr>
</thead>
<tbody>
<% @sat_grades.each do |grade| %>
<tr>
<% if grade.subject.eql? "Total" %>
<td><strong><%= grade.subject %></strong></td>
<td><strong><%= grade.result %></strong></td>
<% else %>
<td><%= grade.subject %></td>
<td><%= grade.result %></td>
<% end %>
</tr>
<% end %>
</tbody>

哪里 @sat_grades这是: @sat_grades = @profile.grades.where(grade_type: :sat) .

我想用这个 lookup_context方法,我是这样想的:
 <% @grades.each do |grade| %>
<% if lookup_context.template_exists?(grade.grade_type, "grades/grade_types", true) %>
<%= render partial: "grade/grade_types/#{grade.grade_type}", locals: {event: event, index: index} %>
<% end %>
<% end %>

我遇到的问题是每个 grade_type有一张不同的 table 。所以 grade_type: :sat属于“SAT”表,同样适用于“CSEC”、“g11”等。

我想不出办法让每个人都拥有这些 grade_types专门在他们的 HTML 表格中呈现,没有很多 lookup_context.template_exists?该 View 中的调用。

如果我必须有一个 lookup_context,它几乎违背了这样做的目的。每人来电 grade_type .

解决这个问题的最佳方法是什么,所以我只有 1 lookup_context调用(如果可能),但它正确地呈现和正确处理所有不同的等级。

最佳答案

对于给定的片段,我将尝试以下操作:

# Render each grade
<%= render(partial: "grade/grade", collection: @grades, locals: {event: event, index: index}) || "There's grade to be displayed" %>
# Render Concated content
<%= content_for :all_grades %>

grade/_grade.html.erb :
 # If a special grade template exists prepare the content to be shown
# but don't display it right now
<% if lookup_context.template_exists?(grade.grade_type, "grades/grade_types", true) %>
<%= render partial: "grade/grade_types/#{grade.grade_type}", locals: {event: event, index: index} %>
<% end %>

# Render the common stuff
...
# Display the special stuff stored for the grade
<%= content_for :grade_table %>
# Repeat previous steps
...

在成绩模板内(例如 grade/grade_types/_g7.html.erb ):
# remove content from previous grades
<% content_for :grade_table, flush: true do %>
...
<% end %>

<% content_for :xxx_xxx, flush: true do %>
...
<% end %>
...

# Concat content for all grades together (flush: false)
<% content_for :all_grades do %>
...
<% end %>

另一种方法可以是演示者,甚至是单表继承。

关于ruby-on-rails - 如何使用 lookup_context 使此 View 尽可能干燥?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39546711/

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