我想知道如何通过重复代码使其变得更容易。与所有模型页面一样,我使用相同类型的代码来显示所有属性值。每次我必须输入相同的部分:
%label.col-sm-3.control-label
= :attribute
.col-sm-9
= @attribute.value
我尝试用帮助器做一些事情,但无法正常工作。我试图制作像(干)这样的东西:
= form_row, :attribute, @attribute.value
那应该生成包括css等在内的完整集。
答案 0 :(得分:2)
助手应该看起来像
def form_row(name, value)
result = label_tag "", name, class: "col-sm-3 control-label"
result << content_tag(:div, value, class: "col-sm-9")
end
然后在您的视图中调用此方法
= form_row :attribute, @attribute.value