Rails - How to set a validation to occur in just one of my own controller methods
I have a password_reset_token field in my model and it just stores an authentication token which will be used to parse a reset password url.
It's going to be nil unless the forgot_password method in my controller is called. It's here the token is generated.
I want a validation to only run here otherwise every time I update a user object validations will complain about the empty token field. How do I do that?开发者_如何学Python
And why do you want to validate it? When the user goes to the forget password form, he will just have to submit his email address I suppose, and it's your turn to generate the authentication token, so basically there is no need to validate it as it won't be an input.
You can skip validation by doing so:
@my_object.save(false)
Of course, use it wisely. Skipping validation can end up with corrupted data being inserted in the database...
精彩评论