在 Rails 中覆盖 ActionView::Base.field_error_proc [英] Overriding ActionView::Base.field_error_proc in Rails
问题描述
现在我的 ActionView::Base.field_error_proc 为
Proc.new do |html_tag, instance|如果 html_tag =~/^<label/或 instance.respond_to?(:object_name)%{<div class="field_with_errors">#{html_tag}</div>}.html_safe别的%{<div class="field_with_errors">#{html_tag}<br/><label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label></div>}.html_safe结尾
当我确实使用 client_side_validations gems 时,我已经修改了它以适应我的一些需求.
现在,'fields_with_error' div 环绕 html_tag(即特定的错误字段).我想知道的是,是否可以修改 div class="fields_with_error" 的位置并将其放在包含 error_field 的 div 之后.
例如,
<%= password_field_tag 'secret', '你的秘密'%>
那么 div class="fields_with_error" 应该被定位为
<%= password_field_tag 'secret', '你的秘密'%><div class="fields_with_error"><label class="message">错误信息</label>
任何帮助都会很有价值.
所以当我使用 twitter bootstrap 时,我通常会用这样的东西制作一个初始化程序
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|html = %(<div class="field_with_errors">#{html_tag}</div>).html_safe# 将 nokogiri gem 添加到 Gemfile表单字段 = ['文本区域','输入','选择']元素 = Nokogiri::HTML::DocumentFragment.parse(html_tag).css "label, " + form_fields.join(', ')element.each 做 |e|如果 e.node_name.eql?'标签'html = %(<div class="控制组错误">#{e}</div>).html_safeelsif form_fields.include?e.node_nameif instance.error_message.kind_of?(Array)html = %(<div class="控制组错误">#{html_tag}<span class="help-inline"> #{instance.error_message.uniq.join(', ')}</span></div>).html_safe别的html = %(<div class="control-group error">#{html_tag}<span class="help-inline"> #{instance.error_message}</span></div>).html_safe结尾结尾结尾html结尾
这只是将常规错误调整为表单中的 twitter 错误:> 所以让一切看起来都很好;>.您可以使用相同的方法.
干杯,如果这有帮助
Right now I have my ActionView::Base.field_error_proc as
Proc.new do |html_tag, instance|
if html_tag =~ /^<label/ or instance.respond_to?(:object_name)
%{<div class="field_with_errors">#{html_tag}</div>}.html_safe
else
%{<div class="field_with_errors">#{html_tag}<br /><label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label></div>}.html_safe
end
I had modified this to accomodate a few of my needs when i did use the client_side_validations gems.
Right now, the 'fields_with_error' div wraps around the html_tag(i.e the error field to be specific). What i would like to know though is if its possible to modify the position of the div class="fields_with_error" and place it right after the div that contains the error_field.
For eg,
<div class="main_div">
<%= password_field_tag 'secret', 'Your secret here' %>
</div>
Then the div class="fields_with_error" should be postioned as
<div class="main_div">
<%= password_field_tag 'secret', 'Your secret here' %>
</div>
<div class="fields_with_error"> <label class="message"> The error message </label> </div>
Any help would be of great value.
So when I work with twitter bootstrap I usually make an initializer with something like this
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
html = %(<div class="field_with_errors">#{html_tag}</div>).html_safe
# add nokogiri gem to Gemfile
form_fields = [
'textarea',
'input',
'select'
]
elements = Nokogiri::HTML::DocumentFragment.parse(html_tag).css "label, " + form_fields.join(', ')
elements.each do |e|
if e.node_name.eql? 'label'
html = %(<div class="control-group error">#{e}</div>).html_safe
elsif form_fields.include? e.node_name
if instance.error_message.kind_of?(Array)
html = %(<div class="control-group error">#{html_tag}<span class="help-inline"> #{instance.error_message.uniq.join(', ')}</span></div>).html_safe
else
html = %(<div class="control-group error">#{html_tag}<span class="help-inline"> #{instance.error_message}</span></div>).html_safe
end
end
end
html
end
This just adjust regular errors to twitter errors in forms :> so makes everything very nice to look at ;>. You can use same approach.
Cheers if this helps
这篇关于在 Rails 中覆盖 ActionView::Base.field_error_proc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!