Unable to display errors for multiple records


#1

Hi,

I’m trying to save multiple records of the same model in one go. Which
is no problem at all. Problem starts when I want to introduce error
handling. The problem is that no record should be saved when one of the
records contains an error. I tried it as follows:

session[:infos].each do |info|
@quest.name = info[“name”]

unless @quest.valid?
@errors_found = true
end
end

if @errors_found
render :action => ‘new’
end

So if an error has occurred I prevent all the records from being saved.
The only problem I’m having is that I can only show errors from the last
record that contained an error. So if one or more of the other records
has an error, I can’t show them to the user. Is their a way to
‘preserve’ the errors added with errors.add in the model, so I can
display the errors for all the records. All suggestions are welcome.

Kind regards,

Nick


#2

There isn’t enough info to give a definitive answer to your problem,
but if displaying the errors is the only problem then I suggest
looking into using a custom error_messages_for in your helper.

This example takes an array of objects and displays a single error
box. Please excuse any ruby syntax errors, I am still learning the
language.

def error_messages_for(object_list, options = {})
options = options.symbolize_keys

object_list.each do |object|
unless object.errors.empty?
content_tag(“div”,
content_tag(
options[:header_tag] || “h2”,
“#{pluralize(object.errors.count, “error”)} prohibited
this whatever from being saved”
) +
content_tag(“p”, “There were problems with the following
fields:”) +
content_tag(“ul”, object.errors.full_messages.collect { |msg|
content_tag(“li”, msg) }),
“id” => options[:id] || “errorExplanation”, “class” =>
options[:class] || “errorExplanation”
)
end
end
end

You can customize this all you want to add more details about each
object.

-John


John S.
Computing Staff - Webmaster
Kavli Institute for Theoretical Physics
University of California, Santa Barbara
removed_email_address@domain.invalid
(805) 893-6307


#3

I see what you are talking about. My last reply did not address the
real problem.

The problem is as soon as an error is found validation stops for all
further objects so a comprehensive validation cannot happen. I have a
single record with a has_many relationship and if an error is found
with the original record then validation does not complete for the
child records.

I have seen this limitation in other MVC frameworks (WebObjects). I
need a solution to this also.

-John


John S.
Computing Staff - Webmaster
Kavli Institute for Theoretical Physics
University of California, Santa Barbara
removed_email_address@domain.invalid
(805) 893-6307


#4

John,

thanks for your answer. It is funny, I had already implemented your
first suggestion to solve the problem of displaying errors for multiple
models. Took a while to find that one. And now as you describe it in
your last response, is exactly the problem I’m having (and apparently
other people too). So it would be really nice if somebody could provide
a solution or at least some helpful comments.

Kind regards,

Nick


#5

Hi Bob,

I think I’m already doing it, see my code snippet at the top. The only
thing is that I’m looping through it multiple times. And all the
previous errors that are thrown are lost, only the errors on the last
record (with errors) are preserved. This is standard behaviour, but
maybe there is a way to override this? Thanks.

Kind regards,

Nick


#6

Quick answer is step in front of AR with your validations using valid?
in your controller before you save it.

Bob S.
http://www.railtie.net/