0

我有一个带有密码、password_confirmation 和 current_password 字段的表单,当我尝试更新它时,它不会更改数据库中的密码。我只想用当前密码更新密码,并在没有当前密码的情况下更新其他字段,如姓名、电子邮件和姓氏……我一直在为此苦苦挣扎,我不知道我在哪里我错了...电子邮件、姓名和姓氏字段可以更新,但密码不能更新,它只会将我重定向到根页面!

这是型号:

 attr_accessible :name,:email,:lastname,:password,:password_confirmation,:current_password,:remember_me

 validates :name,:presence=>true,
              :length=>{:maximum=>50}

 validates :lastname,:presence=>true,
                  :length=>{:maximum=>50}


 def update_with_password(params={})
    current_password = params.delete(:current_password)


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

   result =  if valid_password?(current_password) 
     update_attributes(params)
    else
      self.attributes = params
      self.valid?
      self.errors.add(:current_password, current_password.blank? ? :blank : :invalid)
      false
    end 

    clean_up_passwords
    result
  end      

  def update_without_password(params, *options)
    params.delete(:password)
    params.delete(:password_confirmation)

    result = update_attributes(params, *options)
    clean_up_passwords
    result
  end                   

这是我的注册控制器:

class RegistrationsController < Devise::RegistrationsController 
def edit
@user = current_user
end

def update
@user = User.find(current_user.id)

if @user.update_without_password(params[:user])  
  # Sign in the user by passing validation in case his password changed
  sign_in @user, :bypass => true
  flash[:success]="Profile updated"
  redirect_to edit_user_registration_path(current_user.id)
else
  render "edit"
end

结束结束

我的密码控制器:

class PasswordsController < Devise::PasswordsController
before_filter :authenticate_user!

def edit
@user = current_user
end

def update
@user = current_user
# raise params.inspect
  if @user.update_with_password(params[:user])
    sign_in @user,:bypass=>true
    flash[:success]="You already changed your pass"  
  redirect_to edit_user_password_path(current_user.id)
else
  render :edit
end
end 
end

我的密码编辑视图:

<h2>Change your password</h2>
<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>
<%= f.hidden_field :reset_password_token %>
<div><%= f.label :current_password, "Old password" %><br />
<%= f.password_field :current_password %></div>

<div><%= f.label :password, "New password" %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation, "Confirm new password" %><br />
<%= f.password_field :password_confirmation %></div>  
<div><%= f.submit "Change my password" %></div>
<% end %>
<%= render :partial => "devise/links" %>

我将不胜感激一些帮助我被困在这些一段时间...

4

1 回答 1

0

您不会将用户保存在 Controller 中。

您可以在您的update_without_password方法或控制器中执行此操作。

  def update_with_password(params={})
    current_password = params.delete(:current_password)


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

   result =  if valid_password?(current_password) 
     update_attributes(params)
    else
      self.attributes = params
      self.valid?
      self.errors.add(:current_password, current_password.blank? ? :blank : :invalid)
      false
    end 

    clean_up_passwords
    result
    save
  end 
于 2012-04-05T07:51:07.593 回答