对每个model的field错误信息提取到div的方法

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

module ApplicationHelper
  def error_div(model, field, field_name)
    return unless model
    field = field.is_a?(Symbol) ? field.to_s : field
    errors = model.errors[field]
    return unless errors
    %Q(
    <div class="errors">
    #{errors.is_a?(Array) ? errors.map{|e| field_name + e}.join(",") : field_name << errors}
    </div>
    )
  end
end