2016-09-17 64 views
6

對於初學者來說,這是我試圖複製的觀點:如何使用lookup_context使該視圖儘可能幹?

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方法之前:

<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調用(如果可能),但它能夠正確呈現並正確處理所有不同的等級。

回答

2

在給定的片段我會嘗試以下方法:

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

另一種方法可以是演示者,或者甚至可以是單表I nheritance。

相關問題