Created
October 14, 2011 08:17
-
-
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 )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= 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