To cope with languages other than English, it seems to be necessary to
modify error_messages_for by defining a new application helper. Tricky
stuff for a novice like me!
I need to get all of the text out so that I can make it language
dependent. I have used instance variables at the moment, but will get
them out of the helper once I have a solution. The problem I have is
with the error lines such as the following, which contain a field name
as a prefix:
Terms : You must accept the Terms and Conditions
The following regular expression should work on the above line I think
(i.e. to get rid of “Terms :” so that all remains is the message passed
from the validation :message in the model
[A-Za-z0-9| ]*$
So I am trying to do the following to a line near the bottom of the
script
error_messages = objects.map {|object|
object.errors.full_messages.map {|msg| content_tag(:li, /[A-Za-z0-9|
]*$/.match(msg)) } }
I am obviously making a mistake here, because it gives nothing except a
bullet point, but I am not sure what. Does anyone have an idea where I
am going wrong?
Any help would be really appreciated.
Darren
def my_error_messages_for(*params)
@e1=‘There were problems with the following fields:’
@e2=‘errors’
@e3=‘prohibited this from being saved’
options = params.last.is_a?(Hash) ? params.pop.symbolize_keys : {}
objects = params.collect {|object_name|
instance_variable_get("@#{object_name}") }.compact
count = objects.inject(0) {|sum, object| sum +
object.errors.count }
unless count.zero?
html = {}
[:id, :class].each do |key|
if options.include?(key)
value = options[key]
html[key] = value unless value.blank?
else
html[key] = ‘errorExplanation’
end
end
header_message = "#{pluralize(count, @e2)} #{@e3}"
error_messages = objects.map {|object|
object.errors.full_messages.map {|msg| content_tag(:li, msg) } }
content_tag(:div,
content_tag(options[:header_tag] || :h2, header_message)
<<
content_tag(:p, @e1) <<
content_tag(:p, error_messages),
html
)
else
‘’
end
end
end