如何使用lookup_context将此视图视为DRY?

时间:2016-09-17 12:16:53

标签: ruby-on-rails erb ruby-on-rails-5 actionview

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

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

这是当前表的样子,即在Rails中使用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属于&#34; SAT&#34;表,同样适用于&#34; CSEC&#34;,&#34; g11&#34;等。

我无法想办法让每个grade_types在HTML表格中专门呈现,而不会在该视图中进行大量lookup_context.template_exists?次调用。

如果我必须对每个lookup_context进行grade_type来电,那几乎就会失败这样做的目的。

这是解决此问题的最佳方式,因此我只需要进行1次lookup_context调用(如果可能),但它会正确呈现并正确处理所有不同的成绩。

1 个答案:

答案 0 :(得分:3)

使用给定的片段,我会尝试以下内容:

# 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 %>

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