Forum: Ruby on Rails Unable to display errors for multiple records

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
91308e9bc88cb069fd1bcf88e910d042?d=identicon&s=25 Nick Snels (nicksnels)
on 2006-03-14 20:53
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
858392acd1398aa6071fb1d44cc33842?d=identicon&s=25 John Smilanick (Guest)
on 2006-03-14 21:21
(Received via mailing list)
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 Smilanick
Computing Staff - Webmaster
Kavli Institute for Theoretical Physics
University of California, Santa Barbara
jsmilani@kitp.ucsb.edu
(805) 893-6307
858392acd1398aa6071fb1d44cc33842?d=identicon&s=25 John Smilanick (Guest)
on 2006-03-14 21:54
(Received via mailing list)
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 Smilanick
Computing Staff - Webmaster
Kavli Institute for Theoretical Physics
University of California, Santa Barbara
jsmilani@kitp.ucsb.edu
(805) 893-6307
91308e9bc88cb069fd1bcf88e910d042?d=identicon&s=25 Nick Snels (nicksnels)
on 2006-03-14 22:22
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
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Bob Silva (Guest)
on 2006-03-15 04:57
(Received via mailing list)
Quick answer is step in front of AR with your validations using valid?
in your controller before you save it.


Bob Silva
http://www.railtie.net/
91308e9bc88cb069fd1bcf88e910d042?d=identicon&s=25 Nick Snels (nicksnels)
on 2006-03-15 08:49
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
This topic is locked and can not be replied to.