所以我有一个像这样的input元素。包装元素是关于,你知道的,视觉上的东西。
<div class="input-wrap">
<input class="blah-blah" />
</div>当<input>包含错误时,它将如下所示:
<div class="input-wrap">
<div class="field-with-errors">
<input class="blah-blah" />
</div>
</div>但我想做的是:
<div class="input-wrap field-with-errors">
<input class="blah-blah" />
</div>我找到了这个页面,它非常接近我的问题
Rails 3: "field-with-errors" wrapper changes the page appearance. How to avoid this?
现在我知道我可以扔了
config.action_view.field_error_proc = Proc.new { |html_tag, instance|
"#{html_tag}".html_safe
}以避免在带有错误的<input>标记周围生成环绕标记。但我真正想要做的是,在<input>标记的直接父标记上添加“带错误的字段”类。我可以这样做吗?ActionView是否拥有DOM节点的树形结构?
发布于 2013-03-26 11:08:08
您可以将处理错误的代码放在您喜欢的任何位置,只需将其作为实例变量上的块调用即可,例如
if @instance.errors.any?
<div class="field with errors">
@instance.errors.full_messages.each do |msg|
<p><%= msg %></p>
end
end如果您经常使用它,最好把它拉到一个帮助器中,并将实例变量作为参数传递。
https://stackoverflow.com/questions/15627809
复制相似问题