2012-03-19 68 views
4

我遇到了與我的網站的編輯用戶部分有關的問題。出於某種原因,我試圖編輯用戶時不斷收到錯誤「當前密碼不能爲空」。我們使用設計來管理用戶,但我似乎無法找到任何可能導致此錯誤的代碼。停止設計當前密碼要求

這裏是表單代碼:

- 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

您確保密碼已被設置爲用戶?它是否有助於嘗試使用新創建的用戶? – 2012-03-19 23:07:19

回答

0

我找不到,如果沒有提供current_password工作的解決方案!

如果密碼存在,此解決方案仍檢查有效的密碼和password_confirmation?

因此,我創建update_with_password的user.rb

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