Anand Soni
Anand Soni

Reputation: 5110

update attribute of current_user devise in Rails 3

I am working with devise in Rails3. I am facing one problem to update attribute of user. This is current user, and when I am trying to update user, its not updating. when I see in @user.errors, it says that password can't be blank. I have gone through the devise docs for this and use upate_without_password(params[:user]) but not working for me. Just for to know that I am using Mongodb. I have also gone through the same questions posted in stackoverflow but not working for me. Let me share the code:

in user model: There is validation of password like this :

validates :password,
    presence: true,
    confirmation: true,
    length: { minimum: 5, maximum: 16 }

in user controller:

protect_from_forgery except: :update
  def update
    @user = User.find(params[:id])

    if @user.update_without_password(params[:user])          
      sign_in(current_user, :bypass=>true)
      respond_to do |format|
        format.json {render :json => current_user.to_json(:only =>  [ :status])}
      end
    else         
      respond_to do |format|
        format.json {render :json => @user.errors.to_json}
      end
    end
  end

This is the parameters I am sending in request

Parameters: {"user"=>{"status"=>"verified", "_id"=>"user1"}, "id"=>"user1"}

I have also tried in Rails console. its showing false when I update_attributes but when I inspect the user object its shows me updated.

1.9.3p286 :008 > user.update_attributes({"status"=>"verified"})
 => false 

Any suggestion what I am missing ?

Upvotes: 0

Views: 1646

Answers (1)

UserROR
UserROR

Reputation: 240

Check in your User model whether you have specified this:

validates :password,:presence=>true

change the above validation to be done only on creating a user like this:

validates :password,:presence=>true,:on=>:create

This will skip the password validation while updating

Upvotes: 2

Related Questions