2012-01-26 4 views
1

Я использую Omniauth, чтобы пользователи могли войти (и создать учетную запись) через Facebook, Twitter и Google.Rails 3.2 Omniauth Devise - Добавить пароль - Пропустить текущий пароль

Однако, если пользователь решил больше не использовать эти службы, но продолжать использовать свою учетную запись, они захотят добавить пароль.


Как я могу позволить пользователю добавить пароль к своей учетной записи без ввода current password *


Когда я позволить пользователю перейти к edit_user_registration_path(@user), они видят форму ниже:

= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put, name: 'validation'}) do |f| 
    = devise_error_messages! 

    = f.label :email, class: 'block' 
    = f.email_field :email, class: 'large' 
    %span.infobar Keep this here unless you'd like to change your email 

    = f.label :password, class: 'block' 
    = f.password_field :password, class: 'large' 
    %span.infobar Leave blank if you don't want to change it 

    = f.label :password_confirmation, class: 'block' 
    = f.password_field :password_confirmation, class: 'large' 

    - if @user.password_required? 
    = f.label :current_password, class: 'block' 
    = f.password_field :current_password, class: 'large' 
    %span.infobar We need your current password to confirm changes 

    = f.submit "Update" 

user.rb

def password_required? 
    (authentications.empty? || !password.blank?) && super 
end 

Как вы можете видеть, у меня оно есть так, что если @user.password_required?, тогда отобразите текущее поле пароля. Несмотря на то, что это поле не отображается при попытке создать новый пароль для учетной записи, форма не будет корректно проверяться, так как требуется текущий пароль.

ответ

3

Я просто переписал на RegistrationsController метод # обновление:

class RegistrationsController < Devise::RegistrationsController 
    def update 
    # Override Devise to use update_attributes instead of update_with_password. 
    # This is the only change we make. 
    if resource.update_attributes(params[resource_name]) 
     set_flash_message :notice, :updated 
     # Line below required if using Devise >= 1.2.0 
     sign_in resource_name, resource, :bypass => true 
     redirect_to after_update_path_for(resource) 
    else 
     clean_up_passwords(resource) 
     render_with_scope :edit 
    end 
    end 
end 

Источник: How To: Allow users to edit their account without providing a password

+0

Это не работает для меня, потому что: пароль и: password_confirmation атрибуты, где представлены. В конце концов я удалил их из параметра, если длина пароля была <1. – wintersolutions

Смежные вопросы