开发者

Rails 3.1 - Fields With Errors

I'm using Rails helper methods to build a form, and using validations.

Whenever one of these validations fails, rails wraps the corresponding inputs and lab开发者_Python百科els in a field_with_errors tag. Which is fine.

However, for some reasons rails is wrapping both the input AND the label in different divs, making styling really hard:

eg:

<div class="field">
    <div class="field_with_errors">...label...</div>
    <div class="field_with_errors">..input ...</div>
</div>

and what I need is:

<div class="field">
    <div class="field_with_errors">...label & input...</div>
</div>

Does anyone know how I would achieve this?


One way is to replace the divs with spans, which don't break formatting as they're not block level elements. To do so, put this somewhere in an initializer:

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  "<span class=\"field_with_errors\">#{html_tag}</span>".html_safe 
end

Another way would be to simply make the original divs not display as block level elements, with this line in your CSS file:

.field_with_errors { display: inline-block; }

but this is not fully supported by some of the older browsers (looking at you IE6 and 7).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜