Updating `User` attributes without requiring password

前端 未结 9 1663
醉酒成梦
醉酒成梦 2021-01-30 17:27

Right now, users can edit some their attributes without having to enter their password because my validations are set up like this:

validates :password, :prese         


        
9条回答
  •  旧巷少年郎
    2021-01-30 18:05

    I was having the same problem. I wasn't able to fix it with

    params[:user].delete(:password) if params[:user][:password].blank?
    

    I have only been able to get it to work by doing "update_attribute" on each item individually, e.g.

    if (  @user.update_attribute(:name, params[:user][:name])     && 
          @user.update_attribute(:email, params[:user][:email])   &&
          @user.update_attribute(:avatar, params[:user][:avatar]) &&
          @user.update_attribute(:age, params[:user][:age])       && 
          @user.update_attribute(:location, params[:user][:location]) &&
          @user.update_attribute(:gender, params[:user][:gender]) && 
          @user.update_attribute(:blurb, params[:user][:blurb])   )        
        flash[:success] = "Edit Successful."
        redirect_to @user
    else
      @title = "Edit user info"
      render 'edit'
    end
    

    which is clearly a total hack but its the only way I can figure it out without messing with the validations and deleting the password!

提交回复
热议问题