Marc Pursals
Marc Pursals

Reputation: 792

Controller cannot access to table rails

I'm newbie in Rails and I'm developing an app with 3 controllers/models: Doctors, Patients and reports. Doctor has many Patients, Patient belong to Doctor, Patient has many Reports and Reports belongs to Patient.

To create a Patient from the outside through the API I have this in the controller:

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                                                                                         

This works as expected: from the params I can retrieve the doctor_id and create a new patient related to him.

But the strange thing came out when I do exactly the same with reports. In my controller I have:

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

And this time server crashes and don't retrieve the Patient. pat=nil so pat=Patient.find_by_remember_token(token) don't works.

Does anyone can figure out why this is happening?

Thanks in advance.

SOLUTION:

First of all thanks all for your clues it guide me to the solution. Thanks to debugger gem I could see that the token that "really" was sent to Patient.find_by_remember_token(token) was wrong in a manner. I mean. I was catching the token on the server through

puts token => which return "X6MlhaRLFMoZRkYaGiojfA" (correct token)

BUT through debugger I realize that the real token that was sent was

"\"X6MlhaRLFMoZRkYaGiojfA\"" which is definitely a wrong one so I modified my curl query in next way:

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

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

And then it works... damn 5 hours of delay.

Thanks all!!

Upvotes: 0

Views: 99

Answers (2)

Rails Guy
Rails Guy

Reputation: 3866

Try like this :

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

This way you will be able to know, if your token is coming in request or not. Hope it will help. Thanks

Upvotes: 0

dirtydexter
dirtydexter

Reputation: 1073

try to use this for searching Patient.where(:remember_token => token) this should work.

Upvotes: 0

Related Questions