Getting messages from Model

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

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. [email protected]

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs