2012-07-27 57 views
1

你好,我是在軌道上的紅寶石新手我有驗證舊密碼的問題。如果我要進入編輯帳戶頁面,並且如果我保存舊密碼顯示的驗證,即使編輯帳戶頁面和更改密碼頁面是不同的頁面,但它們共享一個模型。請幫助我。惱人的驗證紅寶石

這裏是我的模型:

validates_presence_of :firstname, :message => 'First Name is required' 
    validates_format_of :firstname, :with => /\A[a-zA-Z]+([a-zA-Z]|\d)*\Z/, :message => 'First name must not contain special characters' 
    validates_presence_of :lastname, :message => 'Last Name is required' 
    validates_format_of :lastname, :with => /\A[a-zA-Z]+([a-zA-Z]|\d)*\Z/, :message => 'Lastname name must not contain special characters' 
    validates_presence_of :email, :message => 'Email Address is required' 
    validates_uniqueness_of :email, :message => 'Email address is already taken, please select a new one' 
    validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "Please enter a valid email address" 
    validates_presence_of :username, :message => 'Username is required' 
    validates_uniqueness_of :username, :message => 'Username is already taken, please select a new one' 
    validates_length_of :username, :within => 3..15, :message => "Username must be at least 3 - 15 characters in length", :on => :create 


    validates_presence_of :password_old, :on => :update, :message => "Old password is required" 

,如果你需要更多的信息只是評論....在此先感謝。

+0

請正確填寫問題。我沒有得到你想說的話。 – 2012-07-27 08:14:44

+0

我有頁面編輯帳戶頁面和更改密碼頁面..他們共享相同的模型中的「user.rb」,每當我把驗證在old_password它顯示在編輯帳戶頁面,這是編輯帳戶是一個不同的頁面,它不' t have oldp_password – Lian 2012-07-27 08:35:10

回答

0

所以你說它更新帳戶信息時不應該檢查密碼。 如果用戶在更改密碼字段中提供了新密碼,則可以編寫一個條件驗證來檢查old_password。

validates_presence_of :password_old, :on => :update, :unless => :current_password.nil?,:message => "Old password is required" 
+0

我沒有:current_password ...我應該怎麼做 – Lian 2012-07-27 09:34:49

+0

current_password就是例子。它意味着用戶將在更改密碼字段中提供的new_password – 2012-07-27 09:41:11