我有一个带有密码,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" %>
我会感谢一些帮助,我被困在这些中一段时间......
答案 0 :(得分: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