Ruby on rails Can“;有错误的字段”;是否附加到引发错误的输入标记的父级?
我有一个这样的输入元素。包装元素是关于,你知道,一个视觉的东西Ruby on rails Can“;有错误的字段”;是否附加到引发错误的输入标记的父级?,ruby-on-rails,ruby-on-rails-3,field-with-errors,Ruby On Rails,Ruby On Rails 3,Field With Errors,我有一个这样的输入元素。包装元素是关于,你知道,一个视觉的东西 <div class="input-wrap"> <input class="blah-blah" /> </div> 避免在出现错误的标记周围制作包装标记。但我真正想做的是,再次在标记的直接父级上添加“fieldwitherrors”类。我可以这样做吗?ActionView是否保存DOM节点的树结构?您可以将用于处理错误的代码放在任意位置,例如,只需将其作为实例变量的一个块调用即可 if
<div class="input-wrap">
<input class="blah-blah" />
</div>
避免在出现错误的
标记周围制作包装标记。但我真正想做的是,再次在
标记的直接父级上添加“fieldwitherrors”类。我可以这样做吗?ActionView是否保存DOM节点的树结构?您可以将用于处理错误的代码放在任意位置,例如,只需将其作为实例变量的一个块调用即可
if @instance.errors.any?
<div class="field with errors">
@instance.errors.full_messages.each do |msg|
<p><%= msg %></p>
end
end
if@instance.errors.any?
@instance.errors.full_messages.each do| msg|
结束
结束
如果您经常使用它,最好将它拉到助手中,并将实例变量作为参数传入。您找到解决方案了吗?对我这样的初学者来说,这似乎是不可能实现的。可能需要另一个助手作为包装器。侧菜单上有一些相关问题。似乎还没有直接的解决办法?我不知道。提供了一个很好的提示。如果您不能从helper函数中更改模板,那么可以通过JS添加数据属性并将类名添加到父级?听起来很糟糕,但这可能是一种方法……你好@beatak,尽管已经有一段时间了,你看到了吗?看起来他实际上是通过在字符串级别进行操作来做到这一点的。。
<div class="input-wrap field-with-errors">
<input class="blah-blah" />
</div>
config.action_view.field_error_proc = Proc.new { |html_tag, instance|
"#{html_tag}".html_safe
}
if @instance.errors.any?
<div class="field with errors">
@instance.errors.full_messages.each do |msg|
<p><%= msg %></p>
end
end