# Getting messages from Model

**URL:** <https://www.ruby-forum.com/t/getting-messages-from-model/114348>\
**Category:** Rails\
**Created:** [September 11, 2007, 5:08pm UTC](https://www.ruby-forum.com/t/getting-messages-from-model/114348 "2007-09-11T17:08:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [September 11, 2007, 5:08pm UTC](https://www.ruby-forum.com/t/getting-messages-from-model/114348/1 "2007-09-11T17:08:56Z")

</div>

In my view I am trying to change my password. When I hit the change  
password button, a method in my controller gets called which calls a  
method in my model to set the password. On entering that Model, however,  
a validation on the format of the password is checked, if it fails it  
kicks back a message, apperently to the Controller? I need to capture or  
recognize that there is a message so I can display that in the veiw and  
let the user know that there password has failed to validate. Any ideas  
on how to do that? Here is the validation check in my Model:

validates\_format\_of :password, :with =\> /^\w+$/, :message =\> ‘ **Password must be alphanumeric** ’, :on =\> :create

Thanks,

-Shandy

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [September 12, 2007, 6:44am UTC](https://www.ruby-forum.com/t/getting-messages-from-model/114348/2 "2007-09-12T06:44:08Z")

</div>

Hi Shandy,

you do something like this in your controller,  
(assume User is the name of model)

class MyController \< ApplicationController  
def create

# do what ever validation you want for the incoming parameters, and

i’ll assume params[:user] contains all attribute necessary for  
creation of user  
user = User.new(params[:user])

```
 begin
   x.save!
 rescue Exception => e
      puts x.errors['password'] # this will print the error

```

message you have given, so do what ever you want with this( i am just  
printing)  
# password is the field name in database table, which can be  
any column you want.  
puts e.message # even this will contains the error  
message.  
end  
end  
end

On Sep 11, 8:08 pm, Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [September 12, 2007, 4:33pm UTC](https://www.ruby-forum.com/t/getting-messages-from-model/114348/3 "2007-09-12T16:33:46Z")

</div>

Hi raghukumar,

Thanks for the suggestion. Its working but it exposed some flaws that I  
had in my code and am trying to work around, but what doesn’t kill us  
makes us stronger right? Thanks again,

-Shandy
