在Ruby on Rails的,哪里有人所说的code这段代码中的http://依据。 github/376389 ?我想的ActiveRecord ::错误延伸与code,它是可有这样我就可以合并的错误消息。
这是不是对的ApplicationController?或LIB?
从github
粘贴 从dev.rubyonrails/attachment/ticket/11394/merge_bang_errors.patch#猴子补丁收集 模块的ActiveRecord 类错误 DEF合并!(错误,选项= {}) fields_to_merge =如果只=选项[:只有] 只要 ELSIF除=选项[:除了] 除了= [除外],除非except.is_a?(阵列) except.map(安培;:to_sym)! errors.entries.map(安培;:第一)。选择做|视| !except.include?(field.to_sym) 结束 其他 errors.entries.map(安培;:第一) 结束 fields_to_merge = [fields_to_merge除非fields_to_merge.is_a?(阵列) fields_to_merge.map(安培;:to_sym)! errors.entries.each做|现场,味精| 如果fields_to_merge.include加场,味精?(field.to_sym) 结束 结束 结束 结束解决方案
您可以直接丢弃到你的初始化目录中的任何ruby文件(创建一个新的,名称任何你想要的)。轨运行的所有文件有每次启动时的时间,并会延长错误在那个时候。
In Ruby on Rails, where does one put the code from this snippet in gist.github/376389? I want to extend ActiveRecord::Errors with the code that's available there so I can merge error messages.
Is this something for ApplicationController? or for lib?
Paste from github
# monkey patch gleaned from dev.rubyonrails/attachment/ticket/11394/merge_bang_errors.patch module ActiveRecord class Errors def merge!(errors, options={}) fields_to_merge = if only=options[:only] only elsif except=options[:except] except = [except] unless except.is_a?(Array) except.map!(&:to_sym) errors.entries.map(&:first).select do |field| !except.include?(field.to_sym) end else errors.entries.map(&:first) end fields_to_merge = [fields_to_merge] unless fields_to_merge.is_a?(Array) fields_to_merge.map!(&:to_sym) errors.entries.each do |field, msg| add field, msg if fields_to_merge.include?(field.to_sym) end end end end解决方案
You can just drop that into any ruby file in your initializers directory (create a new one, name it whatever you want). Rails runs all of the files in there every time it is booted, and will extend Errors at that time.
更多推荐
Ruby on Rails的:延长的ActiveRecord ::错误
发布评论