在Ruby on Rails中,從哪裏將這段代碼放入http://gist.github.com/376389?我想用可用的代碼擴展ActiveRecord :: Errors,以便合併錯誤消息。Ruby on Rails:擴展ActiveRecord :: Errors
這是爲ApplicationController?或爲lib?從github.com
# monkey patch gleaned from http://dev.rubyonrails.org/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
不需要這樣的:'= error_messages_for @user,@ someothererror' – 2013-04-28 00:38:48