Skip to content

Instantly share code, notes, and snippets.

@denmarkin
Created October 14, 2011 08:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denmarkin/1286548 to your computer and use it in GitHub Desktop.
Save denmarkin/1286548 to your computer and use it in GitHub Desktop.
Shared errors for multiple targets (when you need render errors for a form containing nested or unlinked )
def collect_shared_error_messages(target)
targets = []
error_messages = []
if target.kind_of?(Array)
target.collect {|t| targets << t}
else
targets << target
end
targets.each do |target|
target.errors.full_messages.collect {|msg| error_messages << msg} if target.errors.any?
end
error_messages
end
- error_messages = collect_shared_error_messages(target)
- if error_messages.any?
#error_explanation
%h2= "#{pluralize(error_messages.count, "error")} prohibited this record from being saved:"
%ul
- error_messages.each do |msg|
%li= msg
= form_for @something do |f|
= render "shared/error_messages", :target => [@something, @something.else]
# ... your fields
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment