Joh*_*ith 0 ruby ruby-on-rails ruby-on-rails-3
我创建了一个辅助方法来动态地更改表中某些行的背景颜色:我的html如下所示:
<% @treatments.each do |f| %>
<tr class="<%= category_table_row_class(f.category) %>">
.....
和我的助手方法:
module ApplicationHelper
def category_table_row_class(category)
    { 0 => "success", 1 => "warning", 2 => "error", 3 => "info" }[category.id]
    end
end
但我有一些问题,我得到错误:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
我希望有人可以帮助我吗?谢谢
看起来您的一种治疗方法没有类别.你可以做很多事情,有几点建议:
Treatment以强制类别作为开始,我会将以下内容添加到帮助程序中:
module ApplicationHelper
  def category_table_row_class(category)
    return "a_suitable_class" if category.nil?
    { 0 => "success", 1 => "warning", 2 => "error", 3 => "info" }[category.id]
  end
end