2012-04-05 124 views
0

我有一个密码,password_confirmation和current_password字段的窗体,当我尝试更新它,它不会更改数据库中的密码。我只想用当前的密码更新密码,并更新其他字段,如名称,电子邮件和姓氏,而不需要使用当前的密码...我一直在为此苦苦挣扎,我不知道我在哪里, m错了......电子邮件地址,姓名和姓氏可以更新,但密码不是,它只会将我重定向到根页面!无法更新与设计密码?

这是型号:

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" %> 

我将不胜感激一些帮助我一直停留在这一段时间...

回答

0

你不保存您的用户在你的控制器。

您可以在您的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