我创建了一个辅助方法来动态更改表中某些行的背景颜色: 我的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
我希望有人可以帮助我吗?感谢
答案 0 :(得分:5)
看起来您的某种治疗方法没有类别。关于这一点你可以做很多事情,其中有几点建议:
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