Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – Rails验证和’fieldWithErrors’包装选择标记大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
不能得到< div class =“fieldWithErrors”>是正常的行为吗?包含arround选择有验证错误的标签?我个人认为没有理由为什么选择标签应该与其他表格标签(输入,textarea)区别对待.

我确实在该字段的error_messages_for和error_message_on方法中得到错误.

Ps.我已经改变了一点ActionView :: Base.field_error_proc以获得span标签而不是div,但这不是问题.

ActionView::Base.field_error_proc = Proc.new { |html_tag,instance|
   #if I puts html_tag here I only get the <input> tags
   "<span class=\"fieldWithErrors\">#{html_tag}</span>"
}
@H_489_8@

解决方法

问题(对我来说至少)是我的f.SELEct:whatever_id在object.errors对象中寻找一个关键字:whatever_id,当我的验证实际上是:无论如何,不​​是:whatever_id.

我通过改变来解决这个恼人的问题

object.errors.on(@method_Name)

object.errors.on(@method_Name) || object.errors.on(@method_name.gsub(/_id$/,''))

这是差异(针对Rails 2.3.4):

diff --git a/vendor/rails/actionpack/lib/action_view/Helpers/active_record_Helper.rb b/vendor/rails/actionpack/lib/action_view/Helpers/active_record_Helper.rb
index 541899e..5d5b27e 100644
--- a/vendor/rails/actionpack/lib/action_view/Helpers/active_record_Helper.rb
+++ b/vendor/rails/actionpack/lib/action_view/Helpers/active_record_Helper.rb
@@ -247,7 +247,7 @@ module ActionView
       alias_method :tag_without_error_wrapping,:tag
       def tag(name,options)
         if object.respond_to?(:errors) && object.errors.respond_to?(:on)
-          error_wrapping(tag_without_error_wrapping(name,options),object.errors.on(@method_Name))
+          error_wrapping(tag_without_error_wrapping(name,object.errors.on(@method_Name) || object.errors.on(@method_name.gsub(/_id$/,'')))
         else
           tag_without_error_wrapping(name,options)
         end
@@ -256,7 +256,7 @@ module ActionView
       alias_method :content_tag_without_error_wrapping,:content_tag
       def content_tag(name,value,options)
         if object.respond_to?(:errors) && object.errors.respond_to?(:on)
-          error_wrapping(content_tag_without_error_wrapping(name,object.errors.on(@method_Name))
+          error_wrapping(content_tag_without_error_wrapping(name,'')))
         else
           content_tag_without_error_wrapping(name,options)
         end
@H_489_8@ @H_489_8@

大佬总结

以上是大佬教程为你收集整理的ruby-on-rails – Rails验证和’fieldWithErrors’包装选择标记全部内容,希望文章能够帮你解决ruby-on-rails – Rails验证和’fieldWithErrors’包装选择标记所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。