2013-09-22 44 views
2

我試圖用這種形式只更新電子郵件,但驗證失敗Current password can't be blank,需要password_confirmation,我沒有線索如何失去此要求如何僅使用設計更新電子郵件?

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:method => :put, :id=>"email_form"}) do |f| %> 
      <%= devise_error_messages! %> 
      <div><%= f.label :email %> 
       <br/> 
       <%= f.email_field :email, :autofocus => true %></div> 
      <div><%= f.submit "Update" %></div> 
     <% end %> 

UPDATE 這是我更新動作在overrided控制器RegistrationsController <設計:: RegistrationsController

class RegistrationsController < Devise::RegistrationsController 
    def update 
    @user = User.find(current_user.id) 

    successfully_updated = if needs_password?(@user, params) 
     @user.update_with_password(params[:user]) 
    else 
     # remove the virtual current_password attribute update_without_password 
     # doesn't know how to ignore it 
     params[:user].delete(:current_password) 
     @user.update_without_password(params[:user]) 
    end 

    if successfully_updated 
     set_flash_message :notice, :updated 
     # Sign in the user bypassing validation in case his password changed 
     sign_in @user, :bypass => true 
     redirect_to after_update_path_for(@user) 
    else 
     render "edit" 
    end 
    end 

    private 
    def needs_password?(user, params) 
    user.email != params[:user][:email] || 
     params[:user][:password].present? 
    end 
end 
+0

這是已經適合你嗎? – calas

回答

0

只是點到另一個控制器比色器件本身的註冊controntroller,用自己LOGI c用於此更新操作。

您也可以重寫設計註冊的控制器(閱讀設計文檔)。

例子:

# config/routes.rb 
resource :profile  # Singular resource and profile 

# profiles_controller 
def show 
    # render your view with the for 
end 

def update 
    current_user.update(params[:user]) # if rails < 4 use update_attributes instead 
end 

# _form.html.erb 
<%= form_for(current_user, url: profile_path, html: { method: 'PUT' }) do |f| %> 
    ... 

對於第二個選項,覆蓋色器件本身的登記控制器,我真的不喜歡這種做法,因爲在這種情況下,你是不是真的處理註冊,但已經註冊的用戶帳戶:

https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password

您的編輯後:

我看你使用的是山高nd選項。看看needs_password?控制器中的方法

+0

是的,我認爲問題是在needs_password?方法,它可能需要一些額外的檢查 –

相關問題