其他分享
首页 > 其他分享> > html-如果方法不返回数据,则打印一个空表单元格

html-如果方法不返回数据,则打印一个空表单元格

作者:互联网

我在部分视图中有以下代码,以显示每个评估期间学生的平均分数:

<tr class="<%= cycle("odd", "even", name: "students")%>">
    <td>
    <%= link_to "#{student.name}", 
                 student_path({student_group_id: student.student_group_id, id: student.id})%>
    </td>
    <% student.eval_count.times do |i| %>
      <td class="center"><%= student.avg_for_eval(i) %></td>
    <% end %>
    <td class="center"><%= student.avg unless student.avg.nan? %></td>
</tr>

我在student.rb中使用此方法来生成平均分数,但起初我无法弄清楚为什么在没有数据的情况下无法生成空行.

def evals
  evals = self.evaluations.order("eval_number").group_by(&:eval_number)
end

def eval_number_set(index)
  numbers = Evaluation.where('student_id = ?', self.id).uniq.pluck(:eval_number)
  numbers[index]
end

def avg_for_eval(i)
  scores = []
  evals = self.evals.select { |k, v| k == self.eval_number_set(i) }.values.first
  for eval in evals
    scores << eval.score
  end
  evals.empty? #(scores.sum.to_f / scores.size).round(2)  
end

我将方法的最后一行更改为evals.empty?如上图所示,并在浏览器中找到了它:

然后我意识到,由于选择了任何评估,该学生都不在场(对于新学生而言)不是我用来生成代码的数据集的一部分.

正如您在图中看到的那样,问题在于,只有最后一次评估的数据的两个学生的数据没有放在正确的列中-一切都移到了左侧,因为没有< td>由视图代码生成.

那么问题是,如何才能重写方法代码,以便获得相同的输出,但是否为scores.empty?该方法插入“”或“无数据”或其他占位符,以便< td>在视图中打印?

更新

我了解现在情况正在好转.该方法:

def eval_number_set(index)
  numbers = Evaluation.where('student_id = ?', self.id).uniq.pluck(:eval_number)
  numbers[index]
end

正在返回每个学生的评估编号-因此,在下一个方法中使用这些编号时

def avg_for_eval(i)
  scores = []
  evals = self.evals.select { |k, v| k == self.eval_number_set(i) }.values.first
  for eval in evals
    scores << eval.score
  end
  evals.empty? #(scores.sum.to_f / scores.size).round(2)  
end

它只能拉取学生参加的评估.回到画板…

更新2

我将类方法更改如下:

#returns all 'eval_number's for a given group of students 
#as the first student will have been present for all evaluations

def eval_number_set(index)
  numbers = self.student_group.students.first.evals.keys
  # numbers = Evaluation.where('student_id = ?', self.id).uniq.pluck(:eval_number)
  numbers[index]
end

#attempts to match the first present 'eval_number' for a given student against
#the first number in the set of all 'eval_number's and react accordingly
def avg_for_eval(i)
  scores = []
  if self.evals.keys[i] == self.eval_number_set(i)
    "match"
  else
    "no_match"
  end  
end

这与参加每次评估的所有学生相匹配,但与错过评估的学生没有任何匹配.我将代码更改为以下内容

  def avg_for_eval(i)
    scores = []
    if self.evals.keys[i] == self.eval_number_set(i)
      "#{self.evals.keys[i]} vs #{self.eval_number_set(i)}"
    else
      "#{self.evals.keys[i]} vs #{self.eval_number_set(i)}"
    end          
  end

并在浏览器中返回以下内容:

因此,我尝试在if语句中添加一个计数器,以便如果该语句匹配,它将增加尝试的键,否则将保留在同一键上:

  def avg_for_eval(i)
    scores = []
    key_match = 0
    if self.evals.keys[key_match] == self.eval_number_set(i)
      "#{self.evals.keys[i]} vs #{self.eval_number_set(i)}"
      key_match += 1
    else
      "#{self.evals.keys[i]} vs #{self.eval_number_set(i)}"
    end  
  end

产生这个:

并且很有道理-计数增加由于调用方式而没有任何实际效果.但是尽管我认为我现在已经接近想要的效果,但我不确定如何实现它!

更新3

更近一步…我已经进行了一些更改,以便可以反向读取按键,因此可以读取所有数据-但我仍然希望将数据打印在右边,将最新的数据打印出来(请参见下图) ).现在,模型方法代码如下所示:

  def eval_number_set(index)
    numbers = self.student_group.students.first.evals.keys.reverse
    # numbers = Evaluation.where('student_id = ?', self.id).uniq.pluck(:eval_number)
    numbers[index]
  end

  def avg_for_eval(i)
    scores = []
    eval_number = self.eval_number_set(i)
    if self.evals.keys.reverse[i] == eval_number
      for eval in self.evals.values[i] 
        scores << eval.score if self.evals.values[i]
      end
      scores
    else
      "no data"
    end  
  end

它返回以下内容,我已对其进行了注释,以便您可以清楚地了解我正在寻找的内容:

更新4

调用student.evals返回以下内容(对于ID为32的学生):

{29=>[
  #<Evaluation id: 1949, score: 3, created_at: "2013-08-28 09:44:32", updated_at: "2013-08-28 09:44:32", student_id: 32, goal_id: 63, eval_number: 29>, 
  #<Evaluation id: 1950, score: 4, created_at: "2013-08-28 09:44:32", updated_at: "2013-08-28 09:44:32", student_id: 32, goal_id: 64, eval_number: 29>, 
  #<Evaluation id: 1951, score: 5, created_at: "2013-08-28 09:44:32", updated_at: "2013-08-28 09:44:32", student_id: 32, goal_id: 65, eval_number: 29>], 
30=>[
  #<Evaluation id: 1957, score: 3, created_at: "2013-08-28 09:44:43", updated_at: "2013-08-28 09:44:43", student_id: 32, goal_id: 65, eval_number: 30>, 
  #<Evaluation id: 1956, score: 2, created_at: "2013-08-28 09:44:43", updated_at: "2013-08-28 09:44:43", student_id: 32, goal_id: 64, eval_number: 30>, 
  #<Evaluation id: 1955, score: 1, created_at: "2013-08-28 09:44:43", updated_at: "2013-08-28 09:44:43", student_id: 32, goal_id: 63, eval_number: 30>], 
31=>[
  #<Evaluation id: 1968, score: 2, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 70, eval_number: 31>, 
  #<Evaluation id: 1967, score: 2, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 69, eval_number: 31>, 
  #<Evaluation id: 1966, score: 1, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 68, eval_number: 31>, 
  #<Evaluation id: 1965, score: 1, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 67, eval_number: 31>, 
  #<Evaluation id: 1964, score: 1, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 66, eval_number: 31>, 
  #<Evaluation id: 1963, score: 3, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 65, eval_number: 31>, 
  #<Evaluation id: 1962, score: 3, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 64, eval_number: 31>, 
  #<Evaluation id: 1961, score: 3, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 63, eval_number: 31>], 
32=>[
  #<Evaluation id: 1983, score: 3, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 69, eval_number: 32>, 
  #<Evaluation id: 1982, score: 2, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 68, eval_number: 32>, 
  #<Evaluation id: 1981, score: 3, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 67, eval_number: 32>, 
  #<Evaluation id: 1980, score: 4, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 66, eval_number: 32>, 
  #<Evaluation id: 1979, score: 4, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 65, eval_number: 32>, 
  #<Evaluation id: 1978, score: 3, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 64, eval_number: 32>, 
  #<Evaluation id: 1977, score: 3, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 63, eval_number: 32>, 
  #<Evaluation id: 1984, score: 3, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 70, eval_number: 32>]
}

解决方法:

更新

您应该更改eval_count方法或使用常量值制作4个单元格.例如,

4.times do |i|
  <td class="center"><%= student.avg_for_eval(i) %></td>
end

更新结束

您可以尝试以下方法:

def avg_for_eval(i)
  scores = Array.new(4)
  evals = self.evals.select { |k, v| k == self.eval_number_set(i) }.values.first
  evals.each_with_index do |eval, i|
    scores[i] = eval.score
  end
  (scores.sum.to_f / scores.size).round(2)  
end

注意分数数组的初始化.在此方法中,最初创建了[nil,nil,nil,nil].对于只有两个分数的学生,分数将等于[first_score,second_score,nil,nil].

这可能导致错误:方法的结果将是

(first_score + second_score) / 4

标签:html,ruby-on-rails-3,ruby-on-rails,mysql
来源: https://codeday.me/bug/20191122/2062865.html