mar*_*ion 6 ruby-on-rails erb actionview ruby-on-rails-5
对于初学者来说,这是我试图复制的观点:
这是来自该布局的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>
Run Code Online (Sandbox Code Playgroud)
这就是我的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
Run Code Online (Sandbox Code Playgroud)
这就是当前表的样子,即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>
Run Code Online (Sandbox Code Playgroud)
哪里@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 %>
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是每个grade_type都有一个不同的表.因此grade_type: :sat属于"SAT"表,"CSEC","g11"等相同.
我想不出一种方法可以让每个grade_types在HTML表格中专门呈现的内容都没有lookup_context.template_exists?在该视图中进行大量调用.
如果我必须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 %>
Run Code Online (Sandbox Code Playgroud)
之内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
...
Run Code Online (Sandbox Code Playgroud)
在成绩模板内(例如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 %>
Run Code Online (Sandbox Code Playgroud)
另一种方法可以是演示者,甚至可以是单表继承。
| 归档时间: |
|
| 查看次数: |
538 次 |
| 最近记录: |