對於初學者來說,這是我試圖複製的觀點:如何使用lookup_context使該視圖儘可能幹?
這是從佈局的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
方法之前:
<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
有不同的表。因此,grade_type: :sat
屬於「SAT」表中,「CSEC」,「g11」等相同。
我想不出讓這些grade_types
中的每一個專門在其HTML表格中呈現,而沒有在該視圖內調用大量lookup_context.template_exists?
。
如果我必須爲每個grade_type
撥打lookup_context
電話,它幾乎會失敗。
什麼是最好的方法來處理這個問題,所以我只需要1 lookup_context
調用(如果可能),但它能夠正確呈現並正確處理所有不同的等級。