2013-08-16 3 views
0

저는 Rails의 초보자입니다. 3 개의 컨트롤러/모델 (의사, 환자 및 보고서)으로 앱을 개발하고 있습니다. 닥터에는 많은 환자가 있고, 환자는 닥터에 속하며, 환자에게는 많은 보고서와 보고서가 환자에게 속해 있습니다. 컨트롤러가 테이블 레일에 액세스 할 수 없습니다.

이 API를 통해 외부에서 환자를 만들려면 내가 컨트롤러이 있습니다

def create                     
    if doc=params[:patient]                                   
    doctor_id=doc[:doctor]                 
    else                      
    puts 'NO PARAMS' =># this is just to monitor the status in the server                  
    end                      
    doctor=Doctor.find(doctor_id)                
    @patient=doctor.patients.new(                
          name:     doc[:name],        
          email:     doc[:email],        
          sex:     doc[:sex],         
          password:    doc[:password],       
          password_confirmation: doc[:password_confirmation])    

    if @patient.save                   
     render json: { success: true, data: @patient.remember_token, status: :created }  
    else                     
     render json: { success: false, data: @patient.errors, status: :unprocessable_entity } 
    end                      
end                       

이 예상대로 작동 : 나는 doctor_id를 검색하고 그와 관련된 새로운 환자를 만들 수 PARAMS에서.

하지만 정확히 똑같은 보고서를 작성하면 이상한 일이 발생했습니다.

def create                      
    par=params[:report]                   
    token=par[:patient_token]                 
    pat=Patient.find_by_remember_token(token)             
    puts pat         =>#this is to monitor the server             
    last_report=pat.reports.last                 
    puts last_report       =>#this is to monitor the server             
    if ((Time.now-last_report.created_at)/86400).round>0           
    report=create_report(par[:patient_token])             
    report.attributes=par                  
    if report.save                    
     render json: { success: true, data: report, status: :created }       
    else                      
     render json: { success: false, data: report.errors, status: :unprocessable_entity }  
    end                      
    else                       
    last_report.attributes=par                 
    if last_report.save                    
     render json: { success: true, data: last_report, status: :created }      
    else                      
     render json: { success: false, data: last_report.errors, status: :unprocessable_entity } 
    end                      
    end 
end 

그리고이 시간 서버 충돌을하고 환자를 검색하지 않습니다 내 컨트롤러에서 나는 있습니다. pat = nil이므로 pat = Patient.find_by_remember_token (토큰)이 작동하지 않습니다.

왜 이런 일이 발생하는지 파악할 수 있습니까?

미리 감사드립니다.

해결책 : 모든 단서에 대한 모든 감사의

첫째는 솔루션에 저를 안내합니다. 디버거 보석 덕분에 "정말로"Patient.find_by_remember_token (토큰)에 전송 된 토큰이 잘못된 것으로 나타났습니다. 내말은. 내가

을 통해 서버의 토큰을 잡기되었다 토큰 => 어떤 반환 "X6MlhaRLFMoZRkYaGiojfA"(올바른 토큰)

하지만 디버거를 통해 I를 보낸 실제 토큰이

것을 실현 "\"두고 확실히 잘못이다 X6MlhaRLFMoZRkYaGiojfA \ "는"그래서 나는 다음의 방법으로 내 컬 쿼리 수정 :

ORIGINAL CURL: curl -X POST -d 'report[patient_token]="X6MlhaRLFMoZRkYaGiojfA"&repo 

MODIFIED ONE: curl -X POST -d 'report[patient_token]=X6MlhaRLFMoZRkYaGiojfA&repo 

을 그리고 그것은 빌어 먹을 ... 지연 5 시간을 사용할 수 있습니다.

감사합니다!

+0

[: patient_token]은 (는) 존재합니까?, 그 값은 무엇입니까? –

+0

예, 값 : "X6MlhaRLFMoZRkYaGiojfA" –

+0

이므로이 remember_token을 가진 환자가 데이터베이스에 없습니다. –

답변

0

검색을 위해 이것을 사용하려고 시도하면 Patient.where(:remember_token => token)이 작동합니다.

+0

Nop. 그렇지 않습니다. 어쨌든 고마워. –

+0

해당 토큰에 대한 데이터가 있는지 데이터베이스에서 확인 했습니까? – dirtydexter

+0

예. 환자로부터 "remember_token"= "X6MlhaRLFMoZRkYaGiojfA"LIMIT 1 => # 환자 ID (환자) "환자" : 2083, doctor_id : 12, 이름 : [ "Adelia", "O'Hara"], 나이 : 37, 성별 : "남성", 이메일 : "[email protected]", 전화 번호 : nil, password_digest : $ 2a $ 10 $ PkzQOdfkIzHQHfrjJ6xWVe57dUbWyhxfB6SNQ6m.elEP ... ", day_active : nil, remember_token :"X6MlhaRLFMoZRkYaGiojfA ", created_at :"2013-08-10 14:57:22 ", updated_at :"2013-08-10 14:57 :> - –

0

는 다음과 같이하십시오 : 토큰이 요청 여부 오는 경우, 알 수있을 것입니다

def create 
token = params[:report][:patient_token] 
if token 
    pat=Patient.find_by_remember_token(token)             
    puts pat         =>#this is to monitor the server             
else 
    puts "No params" 
end 
end 

이 방법. 희망이 도움이 될 것입니다. 감사합니다

+0

같은 오류. 디버깅하려고하는데 정말 이상합니다. –

관련 문제