2012-04-17 58 views
1

如果我想讓登錄用戶在編輯頁面上更改配置文件信息,但不必像他們目前那樣輸入自己的密碼,我該如何有效更改這在UsersController中?更改before_filter不需要密碼進行配置文件編輯

UsersController:

before_filter :authenticate, :except => [:show, :new, :create] 
    before_filter :authenticate, :only => [:index, :edit, :update, :destroy] 
    before_filter :correct_user, :only => [:edit, :update] 
    before_filter :admin_user, :only => :destroy 



    def edit 
     @title = "Edit user" 
    end 

    def update 
     @user = User.find(params[:id]) 
     if @user.update_attributes(params[:user]) 
      flash[:success] = "Profile updated." 
      redirect_to @user 
     else 
     @title = "Edit user" 
     render 'edit' 
    end 
end 

編輯頁面

 </div> 
    <h1> Confirm Password</h1><br /><br /> 
     <div class="field"> 
     <%= f.label :password, "Enter Password" %>&nbsp;&nbsp;&nbsp;&nbsp; 
     <%= f.password_field :password %> 
    </div> 
    <div class="field"> 
     <%= f.label :password_confirmation, "Confirm Password" %>&nbsp;&nbsp;&nbsp; 
     <%= f.password_field :password_confirmation %> 
    </div> 
    <div class="actions"> 
     <%= f.submit "Submit" %> 
    </div> 
    <% end %> 
    </div> 

回答

0

我不明白你的問題。如果您在登錄時驗證其用戶名和密碼,則不必重新輸入密碼即可編輯其個人資料頁面。登錄後,它應該堅持。你使用任何認證寶石?

+0

嗯,不是我所知道的。只是,在我的編輯(更新)頁面中,我有用於在圖像上傳時確認密碼的表單,除非輸入密碼正確,否則它不起作用。 – Elias7 2012-04-17 16:00:14

+0

很奇怪,我會重寫你的身份驗證。使用身份驗證非常不切實際。看看[Rails Tutorial Sessions](http://ruby.railstutorial.org/chapters/sign-in-sign-out#top)並將其用作模板。 – icantbecool 2012-04-17 17:15:00

0

把這個在您的用戶模型中,如果您正在使用設計的寶石:

def update_with_password(params={}) 
    if current_user && self.password.blank? 
    false 
    if params[:password].blank? 
     params.delete(:password) 
     params.delete(:password_confirmation) if params[:password_confirmation].blank? 
    end 
    else 
    super 
    end 
end 
+0

如果我沒有使用任何認證寶石,我仍然可以完成用戶編輯而無需驗證密碼? – Elias7 2012-04-17 16:03:05

+0

是的,爲什麼不呢?只需檢查密碼是否爲空,並且在update_attributes之前不要對其進行任何類型的檢查。 – 2012-04-17 16:05:19

+0

你在哪裏試圖檢查密碼是否填寫正確? – 2012-04-17 16:05:47

0

這裏是我如何做到這一點的,我現在的工作項目:

用戶模式

validates :password, :length => { :minimum => 8, :maximum => 20}, :unless => Proc.new { |u| u.password_optional? or u.password.blank? } 
    validates :password, :confirmation => true, :unless => Proc.new { |u| u.password_optional? } 

這種方法將取決於你有密碼邏輯......但我的是這樣的:

def password_optional? 
    optional = (encrypted_password.present? && password.blank? && password_changing.blank?) 
    optional 
    end 

用戶控制器 在你想要的密碼是可選的任何操作,請執行以下操作: (假設你有一個@user對象...)

@user.password_optional = true 

我認爲這會在您的更新操作中。