Enhancing Rails Errors 22
So here is my "improved" error_messages_for function.
<%= error_messages_for 'client', { :priority => ['first_name', 'last_name'], :attr_names => {'first_name' => 'Surely, your mother gave you a name?', 'last_name' => 'Suffering from Alzheimers are you?', 'gender' => 'Now I understand why your mother did not name you'}, :defaults=> false, :header => 'You need to correct {count} error(s) before we can save this {object}', :sub_header => 'What on earth were you thinking?' } %>
This will print out:
I admit, this is a bit cumbersome, but it solved the problem I had and for anyone else thats anal about little things like this, it will solve your problems too!
You can download the plugin here. To use the plugin, just extract it to your vendor/plugins directory in your Rails Project. View the README and add some spice to your errors. Let me know if you have ideas for improvement or you have any problems. Enjoy!
UPDATE: Just released version 0.2. This adds the ability to pass in an array of object names and have the errors consolidated into one container.
# put this at the top of the error_messages_for method
object_names.each { |name| objects << instance_variable_get("@#{name}") if !instance_variable_get("@#{name}").errors.nil? }content_tag("ul", errors.collect { |value| content_tag("li", ((options[:attr_names].has_key?(value[0])) ? "#{options[:attr_names][value[0]]}" + (options[:defaults] ? " #{value[1]}" : '') : value[1] =~ /^\^/ ? "#{value[1][1..-1]}" : "#{value[0].humanize} #{value[1]}")) }),replacing the 'old' one :content_tag("ul", errors.collect { |value| content_tag("li", ((options[:attr_names].has_key?(value[0])) ? "#{options[:attr_names][value[0]]}" + (options[:defaults] ? " #{value[1]}" : '') : "#{value[0].humanize} #{value[1]}")) }),object_names.each { |name| objects instance_variable_get("@#{name}") if !instance_variable_get("@#{name}").nil? && !instance_variable_get("@#{name}").errors.nil? }