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 -- Posted via http://www.ruby-forum.com/.
John Smilanick
2006-Mar-14 20:20 UTC
[Rails] Unable to display errors for multiple records
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 On Mar 14, 2006, at 11:53 AM, Nick Snels wrote:> 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060314/62e540f3/attachment-0001.html
John Smilanick
2006-Mar-14 20:53 UTC
[Rails] Unable to display errors for multiple records
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 On Mar 14, 2006, at 11:53 AM, Nick Snels wrote:> 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060314/1bf117eb/attachment.html
Nick Snels
2006-Mar-14 21:23 UTC
[Rails] Re: Unable to display errors for multiple records
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 -- Posted via http://www.ruby-forum.com/.
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/ On Tuesday, March 14, 2006, at 10:22 PM, Nick Snels wrote:>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 > > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails-- Posted with http://DevLists.com. Sign up and save your time!
Nick Snels
2006-Mar-15 07:49 UTC
[Rails] Re: Unable to display errors for multiple records
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 -- Posted via http://www.ruby-forum.com/.