2011-07-01 31 views
29

Tôi có một bộ điều khiển tùy chỉnh xử lý việc chỉnh sửa mật khẩu người dùng dựa trên mã số here.Devise Not Validating Password/Xác nhận mật khẩu

tài khoản Mẫu

attr_accessible :password, :password_confirmation, :username, :login 
... 
devise :database_authenticatable, 
     :lockable, 
     :registerable, 
     :recoverable, 
     :rememberable, 
     :trackable 

PasswordsController

expose(:user) { current_user } 

def update 
    if user.update_with_password(params[:user]) 
    sign_in(user, :bypass => true) 
    flash[:notice] = "success" 
    else 
    render :edit 
    end 
end 

chỉnh sửa hình thức mật khẩu của tôi nằm here.

Vấn đề là không có vấn đề gì tôi nhập (hoặc không nhập cho vấn đề đó) vào biểu mẫu chỉnh sửa mật khẩu, Phương pháp flash "thành công" được hiển thị.

+0

Mọi thứ có hoạt động không? nếu trường xác nhận không khớp hoặc current_password là sai, nó không thay đổi mật khẩu? – Dex

+0

Nó vẫn thay đổi nó cho bất cứ điều gì là trong: mật khẩu bất kể những gì (nếu bất cứ điều gì) là trong: password_confirmation. –

+0

điều gì sẽ xảy ra nếu bạn thay đổi câu lệnh if thành 'if params [: user] [: password] == params [: user] [: password_confirmation]' và đặt 'user.update_with_password (params [: user])' vào phần thân ? –

Trả lời

62

Nếu bạn muốn Devise thực hiện xác thực, bạn cần thêm mô-đun :validatable vào mô hình của mình. Điều này là khá dễ dàng để làm, chỉ cần thêm :validatable vào danh sách các mô-đun trong devise cuộc gọi, vì vậy mô hình của bạn nói:

devise 
    :database_authenticatable, 
    :lockable, 
    :registerable, 
    :recoverable, 
    :rememberable, 
    :trackable, 
    :validatable 

Điều này sẽ làm devise thêm hợp lệ.

Một cách dễ dàng khác là thêm xác thực của riêng bạn. Nếu bạn chỉ muốn xác nhận rằng thư xác nhận mật khẩu phù hợp, bạn có thể thêm một xác nhận validates_confirmation_of bằng cách thêm này để mô hình của bạn:

validates_confirmation_of :password 

Tôi hy vọng điều này sẽ giúp.

+1

'validates_confirmation_of: mật khẩu' là những gì tôi đã mất tích –

1

tìm đối tượng của bạn để cập nhật trong bộ điều khiển.

user = User.find_by_id(params[:id]) 
    unless user.blank? 
     if user.update_attributes(params[:user]) 
     flash[:notice] = "User updated successfully." 
     redirect_to "somwhere" 
     else 
     render :action => 'edit' 
     end 
    else 
     render :action => 'edit' 
    end 

nếu bạn không muốn cập nhật mật khẩu cũ sau đó thêm những dòng trước updation để mã mới sẽ là:

user = User.find_by_id(params[:id]) 
     unless user.blank? 
      params[:user].delete(:password) if params[:user][:password].blank? 
      params[:user].delete(:password_confirmation) if params[:user][:password_confirmation].blank? 
if user.update_attributes(params[:user]) 
      flash[:notice] = "User updated successfully." 
      redirect_to "somwhere" 
      else 
      render :action => 'edit' 
      end 
     else 
      render :action => 'edit' 
     end 

ghi somthing như thế này trong mô hình user.rb

devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, :locakable 
4

Tôi nghĩ rằng bạn quên để khởi tạo tham số mạnh trong application_controller.rb trong đường ray 4

before_action: configure_permitted_parameters, nếu:: devise_controller? được bảo vệ

def configure_permitted_parameters  
    devise_parameter_sanitizer.for(:sign_up){|u|u.permit(:email,:password,:password_confirmation)} 
end 
+1

Đó là những gì bit tôi, tôi quên thêm' password_confirmation' vào danh sách các thông số vệ sinh, cảm ơn! – hernandes

Các vấn đề liên quan