Ruby on Rails
Friday, August 1, 2014
I had this same problem a while ago. The issue comes in the update_attributes since it wants to update all of the attributes that are being passed to it, which is including an empty password. To fix it you'll need to do two things. First change the validates to only validate if a password is being passed.
That should get you where you want to go.
On Friday, August 1, 2014 8:44:53 AM UTC-6, Ruby-Forum.com User wrote:
-- validates :password, length: { minimum: 6 }, :if => :password
Second, remove the parameters for password if the are blank on your update method.
if params[:password].blank?
params.delete(:password)
end
On Friday, August 1, 2014 8:44:53 AM UTC-6, Ruby-Forum.com User wrote:
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.
--
Posted via http://www.ruby-forum.com/.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ec3f3610-eb6d-446f-9543-fcb51342f4c7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment