2012-01-09 3 views
4

내 응용 프로그램에는 두 가지 클래스가 있습니다. 그들 중 하나 (투자자)의 속성을 업데이트하려고합니다. 그들은 devise를 통해 하나의 양식을 사용하여 생성됩니다. 결과적으로 투자자가 자신의 계좌 정보를 업데이트하려고 시도 할 때 양식을 작성하고 비밀번호를 확인해야합니다. 해당 필드에 암호를 변경하지 않고 암호를 입력하지 않고도 (이름, 성 등) 편집 할 수있게하고 싶습니다. 내 등록 컨트롤러Ruby on Rails를 업데이트하면 비밀번호없이 사용자 속성을 변경할 수 있습니다.

class RegistrationsController < Devise::RegistrationsController 

    layout "index" 

    protected 

    def after_sign_up_path_for(resource) 
    new_user_url(:account => resource) 
    end 
end 

여기

는 투자자 컨트롤러 여기

def update 

    session[:investor_params] ||= {} 
    session[:investor_params].deep_merge!(params[:investor]) if params[:investor].present? 

    @investor.attributes = session[:investor_params] 

    params[:investor_params].delete(:password) if params[:investor_params][:password].blank? 
    params[:investor_params].delete(:password_confirmation) if params[:investor_params][:password_confirmation].blank? 
    respond_to do |format| 
    if @investor.update_attributes(params[:investor_params]) 
     session[:investor_params] = nil 
     sign_in(@investor.account, :bypass => true) 
     format.html { redirect_to(projects_url, :notice => 'Investor was successfully updated.') } 
     format.xml { render :xml => @investor, :status => :created, :location => @investor } 

    else 
     format.html { render :action => "edit", :layout => "investor" } 
     format.xml { render :xml => @investor.errors, :status => :unprocessable_entity } 
    end 
    end 
    end 

내 업데이트 방법입니다 것은 여기 내 routes.rb

devise_for :accounts, :controllers => { 
    :registrations => 'registrations', 
    :passwords  => 'passwords', 
    :sessions  => 'sessions' } 
    devise_scope :account do 
    root :to => 'registrations#new' 

입니다 그리고 이것은의 일부입니다 계정 모델의 속성을 참조하는 내 투자자 모델 여기

class Investor < ActiveRecord::Base 

    has_one :account, :as => :profile 

    accepts_nested_attributes_for :account 

내가 고안의 GitHub의의의 repo에 대한 권장 사항을 시도했지만 나는 그것이 나를 위해 작업을 진행 할 수 없습니다
class Account < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me, :invited_by, :invited_by_id 

참조되는 계정 모델의 부분이다.

제안 사항이 있거나 누락 된 사항이 있으면 알려 주시기 바랍니다.

답변

3

현재 매개 변수에 current_password를 보내시겠습니까? (심지어 비어있는 경우에도?) 일반적으로 암호를 요구할 때 #update_with_password을 사용하기를 원합니다. 따라서 update_attributes를 사용하여 이것을 얻는 이유를 모르겠습니다.

+0

update_with_password 메서드가 포함 된 장치 파일의 위치를 ​​알 수 없습니다. 고안은 보석으로 설치되어 있지만, 내가 어디에서 찾을 수 있는지 아십니까? – seanpat09

+0

bundler/textmate를 사용하는 경우 명령 줄에서 "mate $ (bundle show devise)"를 사용하여 보석 원본을 편집 할 수 있습니다. 그렇지 않으면 $ GEM_HOME 경로에 있어야합니다. 보석 소스에서 메소드를 볼 수 있습니다 : lib/devise/models/database_authenticatable.rb 업데이트하는 동안 암호가 필요할 경우에만이 방법을 사용하는 것이 좋습니다. 실수로 params [: current_password]를 전달했는지 확인해보십시오. – RobH

+0

감사합니다. 지금까지 많은 도움을 주신 데 대해 감사드립니다. 암호 없이도 업데이트하고 싶습니다. 내 양식에는 암호 및 password_confirmation을 포함하여 업데이트 할 수있는 모든 필드가 있습니다. 암호 및 password_confirmation 필드에 아무것도 넣지 않고도 나머지 정보를 업데이트 할 수 있기를 원합니다. 내가 그렇게 할 때, 그들이 비어있을 수 없다는 오류 메시지가 나옵니다. 또한, : current_password는 프로젝트 파일 어디에도 정의되어 있지 않지만 devise gem source에 정의되어 있습니다. 그게 네가 언급 한 것인가? – seanpat09

관련 문제