【发布时间】:2012-08-27 08:06:52
【问题描述】:
这是用于显示错误消息的代码
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance_tag|
unless html_tag =~ /^<label/
"<span class=\"field_with_errors\">#{html_tag}</span>".html_safe
else
"#{html_tag}".html_safe
end
end
但是,最好不要同时使用unless 和else。所以我做到了
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance_tag|
if html_tag !=~ /^<label/
"#{html_tag}".html_safe
else
"<span class=\"field_with_errors\">#{html_tag}</span>".html_safe
end
end
它不工作。
我知道这是因为"!=~"。
那么,我该如何更改它以使其正常工作?
【问题讨论】:
-
unless有什么问题尽管我不明白为什么人们不使用unless和if一样有效,你可以使用not尽管前面有if之类的not(html_tag =~ /^<label/)希望这个帮助 -
操作员是
!~,但是你不想切换块中完成的操作。
标签: ruby