2

사용자를 등록한 컨트롤러를 겹쳐 쓰고 있습니다. 사용자가 도시를 변경했는지 확인한 다음, 변경된 경우 Geokit 오브젝트를 다시로드하십시오.Object.attribute_changed? 레일즈 컨트롤러에서 작동하지 않습니다.

session[:geo_location] = User.geocode(resource.city) if resource.city_changed? 

문제가되지 않습니다. 아래 코드는 반환되지 않습니다. if resource.city_changed?을 제거하면 리소스가 변경되었는지 여부에 관계없이 모든 경우에 해당됩니다.

User::RegistrationsController < Devise::RegistrationsController 

    def update 
    self.resource = resource_class.to_adapter.get!(send(:"current_# {resource_name}").to_key)  
    if resource.update_with_password(params[resource_name]) 

     session[:geo_location] = User.geocode(resource.city) if resource.city_changed? 

     set_flash_message :notice, :updated if is_navigational_format? 
     sign_in resource_name, resource, :bypass => true 
     respond_with resource, :location => after_update_path_for(resource) 
    else 
     clean_up_passwords(resource) 
     respond_with_navigational(resource){ render_with_scope :edit } 
    end 

    end 

end 

답변

4

update_with_password 전화 save합니까? 그렇다면 모델의 "더티"상태와 모든 속성을 지 웁니다. 속성을 설정하는 후에 resource.city_changed?을 호출해야합니다. 보다 앞에을 호출하기 전에 save을 호출해야합니다.

+0

어떻게 할 수 있습니까? – Martin

+0

원본 질문을 'update_with_password' 메소드의 코드로 업데이트하십시오. – Brandan