Now i want to get the errors of Model(like some error of
validate_xxx_of )
How can i do? it seems that no error object on Controllers by API
docments,
Thanks.
Now i want to get the errors of Model(like some error of
validate_xxx_of )
How can i do? it seems that no error object on Controllers by API
docments,
Thanks.
ModelName.errors.add(attribute, message)
ModelName.errors.add_to_base(message)
see:
http://api.rubyonrails.org/classes/ActiveRecord/Errors.html
Anson wrote:
Now i want to get the errors of Model(like some error of
validate_xxx_of )How can i do? it seems that no error object on Controllers by API
docments,Thanks.
object_name.errors will give all these type errors.
Suppose if Student is the class name,
@student = Student.new(params[:student])
@student.save
In the above code, if there is any error while saving, you can get the
errors in the controller like,
@student.errors
It will return the object of ActiveRecord::Errors class.
If you want to get the errors in array, you can use like,
@student.errors.full_messages
Refer the ActiveRecord::Errors class in the ROR API
I have 2 validations on a single object, like this in my user model:
validates_length_of :password, :within => 6…12
validates_presence_of :password
When I do a unit test to ensure a missing password is responded to
correctly, for example:
@error_messages = ActiveRecord::Errors.default_error_messages
user = User.new(:name => ‘my test user’,
assert !user.valid?
assert_equal @error_messages[:blank], user.errors.on(:password)
I get a message such as:
test_create_user_passwords_missing(UserTest)
[test/unit/user_test.rb:143]:
<“can’t be blank”> expected but was
<[“is too short (minimum is 6 characters)”, “can’t be blank”]>.
I have 2 questions:
All help appreciated!
Thanks
Mark.
Karthi kn wrote:
Anson wrote:
Now i want to get the errors of Model(like some error of
validate_xxx_of )How can i do? it seems that no error object on Controllers by API
docments,Thanks.
object_name.errors will give all these type errors.
Suppose if Student is the class name,
@student = Student.new(params[:student])
@student.saveIn the above code, if there is any error while saving, you can get the
errors in the controller like,@student.errors
It will return the object of ActiveRecord::Errors class.
If you want to get the errors in array, you can use like,
@student.errors.full_messages
Refer the ActiveRecord::Errors class in the ROR API
- Karthi
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs