I have a User model:
create_table “users”, force: true do |t|
t.string “name”
t.string “email”
t.datetime “created_at”
t.datetime “updated_at”
t.string “password_digest”
end
add_index “users”, [“name”], name: “index_users_on_name”
Then, I have the following validation:
class User < ActiveRecord::Base
has_secure_password
validates :password, length: {minimum: 4}
end
Then, I update in my controller the name and email of a certain user:
…
logger.debug(’++++++++ new_values after: ‘+new_values.inspect)
if @user.update_attributes(new_values)
logger.debug(’+++++++++++++ SUCCESS’)
…
end
(new_values is of type ActionController::Parameters)
My log shows
++++++++ new_values after: {“name”=>“1111”, “email”=>“1111”}
However, the update_attributes fails, and the error message says:
Password is too short (minimum is 4 characters)
What I don’t understand is, that I don’t supply a new password. Why,
then, is password validation triggered here? And how should I implement
this?
Background for this question:
This code is executed only, when a user wants to edit his profile data
(which, for the time being, consist only of name and email). It is
ensured that only a logged in user can execute this code, and only for
his own data. Therefore, I don’t require to enter the password. He had
entered it anyway when logging in.