{"id":146,"date":"2006-08-06T10:14:50","date_gmt":"2006-08-06T15:14:50","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/08\/06\/raising-model-errors-in-rails\/"},"modified":"2006-08-06T10:14:50","modified_gmt":"2006-08-06T15:14:50","slug":"raising-model-errors-in-rails","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2006\/08\/06\/raising-model-errors-in-rails\/","title":{"rendered":"Raising Model Errors In Rails"},"content":{"rendered":"<p>Recently I covered rails&#8217; <a href=\"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/07\/29\/rails-model-validators\/\">model validators<\/a>.  I wanted to add a few more details regarding the validation methods.  If a validation fails you can display an error message in the rhtml view by using the following code:<\/p>\n<pre>\n&lt;%= error_messages_for 'user' %&gt;\n<\/pre>\n<p>The parameter for error_messages_for is the name of an instance variable.  An instance variable are those with the at sign, in this case @user.  When creating, updating, or saving the @user instance if an error occurs the error_messages_for will create the HTML to display the error.<\/p>\n<p>You can also directly add error messages to the model depending some business logic.  In the controller you can add error messages to a model as follows:<\/p>\n<pre>\n@user.errors.add field_name, error_message\n<\/pre>\n<p>One thing to note is that adding errors to the model does not invalidate the model so you might want to save only if there are no errors.<\/p>\n<pre>\nif @user.errors.empty? && @user.save\n  # successfully saved with no errors\nelse\n  # Errors occurred, redirect to form\nend\n<\/pre>\n<p>Technorati Tags: <a href=\"http:\/\/technorati.com\/tag\/ruby\" rel=\"tag\">ruby<\/a>, <a href=\"http:\/\/technorati.com\/tag\/ruby+on+rails\" rel=\"tag\"> ruby on rails<\/a>, <a href=\"http:\/\/technorati.com\/tag\/rubyonrails\" rel=\"tag\"> rubyonrails<\/a>, <a href=\"http:\/\/technorati.com\/tag\/model\" rel=\"tag\"> model<\/a>, <a href=\"http:\/\/technorati.com\/tag\/activerecord\" rel=\"tag\"> activerecord<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I covered rails&#8217; model validators. I wanted to add a few more details regarding the validation methods. If a validation fails you can display an error message in the rhtml view by using the following code: &lt;%= error_messages_for &#8216;user&#8217; %&gt; The parameter for error_messages_for is the name of an instance variable. An instance variable [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[22,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-2m","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/146"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=146"}],"version-history":[{"count":0,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/146\/revisions"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=146"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}