In my User model i have the following validations:
validates :password, :presence => true, :length => { minimum: 8 }, :on => :update_settings
if i don’t use the :on validator it works work across the board. However with the :on I don’t get any validation when executing the specified actions.
The form is going to: action="/users/1/update_settings"
The route is set up: user_update_settings PUT /users/:user_id/update_settings(.:format) users#update_settings
I have checked the guidehere. The same validation works on standard actions like :create but not on my own actions.
Can you see what else am I missing to make this validation valid? Thanks!
I think you’re mixed up about what the
:onoption is for. It is not for specifying actions on the controller, it is for specifying actions on the record. It would go totally against the MVC principle to tie controller actions to model validations in the way you’re trying to do here.Check the docs:
So the
onoption takes one of only three possible values::save(the default),:updateor:create.Given the name of your action (
update_settings), I’m going to assume that in that action you are callingupdate_attributes(orupdate_attribute) on the model, in which case you should just use:on => :updatein your validator. Note that this means that it will also apply the validation in any other controller actions that update the record, since the condition is not specific to any particular controller action.