我的网站编辑用户部分出现问题。出于某种原因,我在尝试编辑用户时不断收到错误“当前密码不能为空”。我们使用devise来管理用户,但我似乎无法在任何地方找到会产生此错误的代码。
以下是表单的代码:
- semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |form|
= devise_error_messages!
= form.semantic_errors
= form.input :first
= form.input :last
= form.input :birth_year, as: :select, collection: User.birth_range.to_a.reverse
%i= t('users.edit.cast_biometrics_hint')
= form.input :gender, as: :select, collection: gender_options, include_blank: false
= form.input :eye_color, as: :select, collection: eye_color_options, required: false
= form.input :hair_color, as: :select, collection: hair_color_options, required: false
= form.input :ethnicity, as: :select, collection: ethnicity_options, required: false
%li.select.optional#user_height_input
%label{for: 'user_height'} Height
%select#user_height_ft{name: 'user[height_ft]'}
= options_for_select 0..9, resource.height_ft
%span ft
%select#user_height_in{name: 'user[height_in]'}
= options_for_select 0..11, resource.height_in
%span in
= form.buttons
答案 0 :(得分:8)
设计维基中有更多关于此的信息。 https://github.com/plataformatec/devise/wiki/How-To%3a-Allow-users-to-edit-their-account-without-providing-a-password
答案 1 :(得分:0)
答案 2 :(得分:0)
我找不到一个没有提供current_password的解决方案!
如果存在密码,此解决方案仍会检查有效密码和password_confirmation吗?
所以我在user.rb
中创建了update_with_password的更新版本def admin_update_with_password(params, *options)
current_password = params.delete(:current_password)
if params[:password].blank?
params.delete(:password)
params.delete(:password_confirmation) if params[:password_confirmation].blank?
end
result = unless update_attributes(params, *options)
self.assign_attributes(params, *options)
self.valid?
self.errors.add(:current_password, current_password.blank? ? :blank : :invalid)
false
end
clean_up_passwords
result
end
和我的users_controller.rb
def update
@user = User.find(params[:id])
email_changed = @user.email != params[:user][:email]
password_changed = !params[:user][:password].empty?
successfully_updated = if email_changed or password_changed
@user.admin_update_with_password(params[:user])
else
@user.update_without_password(params[:user])
end
if successfully_updated
flash[:notice] = "User updated successfully"
redirect_to redirect_path
else
render "edit"
end
end