Ruby on rails 3 使用rails+;诺科吉里
我在rails应用程序中的初始值设定项中有以下内容,该应用程序使用Twitter引导,以便在字段验证失败时删除rails应用的带有错误的Ruby on rails 3 使用rails+;诺科吉里,ruby-on-rails-3,validation,xpath,twitter-bootstrap,nokogiri,Ruby On Rails 3,Validation,Xpath,Twitter Bootstrap,Nokogiri,我在rails应用程序中的初始值设定项中有以下内容,该应用程序使用Twitter引导,以便在字段验证失败时删除rails应用的带有错误的div.field\u,并且初始值设定项在错误的输入字段后添加帮助/验证文本: require 'nokogiri' ActionView::Base.field_error_proc = Proc.new do |html_tag, instance| html = %(<div class="field_with_errors">#{html
div.field\u
,并且初始值设定项在错误的输入字段后添加帮助/验证文本:
require 'nokogiri'
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
html = %(<div class="field_with_errors">#{html_tag}</div>).html_safe
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 = %(#{e}).html_safe
elsif form_fields.include? e.node_name
if instance.error_message.kind_of?(Array)
html = %(#{e}<span class="help-inline"> #{instance.error_message.join(',')}</span>).html_safe
else
html = %(#{e}<span class="help-inline"> #{instance.error_message}</span>).html_safe
end
end
end
html
end
但是我需要向我的初始值设定项添加一些内容,以便它将.error
类添加到div.control-group
中,如下所示:
<div class="control-group error">
<label class="control-label" for="post_message">Message</label>
<div class="controls">
<input id="post_message" name="post[message]" required="required" size="30" type="text" value="" /><span class="help-inline"> can't be blank</span>
</div>
</div>
消息
不能是空白的
解决方案可能需要考虑到这样一个事实,即每个验证错误可能有多个标签和输入,这些标签和输入都位于同一个div.control-group
(例如单选按钮/复选框/2个文本字段并排)
我假设它需要某种类型的e.at_xpath()
来找到div.control-group
父对象,并将.error
类添加到它中,但我不确定如何做到这一点
有人能帮忙吗
PS这可能都可以使用formtastic或simple_form gems,但如果可能的话,我宁愿使用自己的html
编辑
如果我把
e['class']='foo'
放在If e.node\u name.eql标签“
部分,然后它将类应用于标签,所以我想我只需要找到e
的父标记,然后对其应用.error
类,但我无法计算从标签到其div.control-group
父类的xpath是什么;没有点、斜线或任何东西的组合,但xpath不是我的强项。不是最好的解决方案,而是有效的解决方法,因为我更喜欢html/js而不是rails
->使用jQuery选择器和addClass
http://api.jquery.com/addClass/
$("div.control-group:contains('can\\'t be blank')").addClass("error");
我现在成功了
根据这个答案,这是不可能的:
下面是我所做的:
config/initializers/bootstrap.rb
require 'nokogiri'
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
html = %(<div class="field_with_errors">#{html_tag}</div>).html_safe
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'
# wrap erroneous label
html = %(<span class='field_with_errors'>#{e}</span>).html_safe
elsif form_fields.include? e.node_name
# wrap erroneous field
if instance.error_message.kind_of?(Array)
html = %(<span class='field_with_errors'>#{e}<span class="help-inline"> #{instance.error_message.join(',')}</span></span>).html_safe
else
html = %(<span class='field_with_errors'>#{e}<span class="help-inline"> #{instance.error_message}</span></span>).html_safe
end
end
end
html
end
希望这对其他人有所帮助。如果您不需要在错误字段旁边显示错误消息,则只需将以下内容添加到
资产/样式表/应用程序.css.scss
:
.field_with_errors {
@extend .control-group.error;
display: inline;
}
谢谢,但我正在尝试更新用户使用js时输出的html,或者当用户提交表单时,它在我的js验证中遗漏了一些内容,但被服务器端验证捕获。
.field_with_errors {
@extend .control-group.error;
}
.field_with_errors {
@extend .control-group.error;
display: inline;
}