【问题标题】:Controller cannot access to table rails控制器无法接触到桌轨
【发布时间】:2013-08-16 09:03:07
【问题描述】:

我是 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                                                                                         

这按预期工作:从参数中,我可以检索医生 ID 并创建一个与他相关的新患者。

但是当我对报告做同样的事情时,奇怪的事情就出现了。在我的控制器中,我有:

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(token) 不起作用。

有谁知道为什么会这样?

提前致谢。

解决方案:

首先感谢您提供的线索,它引导我找到解决方案。感谢调试器 gem,我可以看到“真正”发送给 Patient.find_by_remember_token(token) 的令牌在某种程度上是错误的。我是说。我正在通过

在服务器上捕获令牌

puts token => 返回 "X6MlhaRLFMoZRkYaGiojfA" (正确的 token)

但是通过调试器我意识到发送的真正令牌是

"\"X6MlhaRLFMoZRkYaGiojfA\"" 这绝对是错误的,所以我用下一种方式修改了我的 curl 查询:

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

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

然后它就起作用了……该死的延迟 5 小时。

谢谢大家!!

【问题讨论】:

  • par[:patient_token] 是否存在?它的值是多少?
  • 是的,值:“X6MlhaRLFMoZRkYaGiojfA”
  • 因此很明显,您的数据库中不存在具有此 remember_token 的患者。
  • 不,它存在。 1.9.3p448 :001 > Patient.find_by_remember_token("X6MlhaRLFMoZRkYaGiojfA") 患者负载 (1.4ms) 选择“患者”。* 从“患者”中选择“患者”。“remember_token” = 'X6MlhaRLFMoZRkYaGiojfA' LIMIT 1 => #

标签: ruby-on-rails ruby


【解决方案1】:

尝试使用它来搜索Patient.where(:remember_token => token) 这应该可以。

【讨论】:

  • 没有。它没有。还是谢谢。
  • 您是否在数据库中验证了该特定令牌的数据存在?
  • 是的,我做到了。 1.9.3p448 :001 > Patient.find_by_remember_token("X6MlhaRLFMoZRkYaGiojfA") 患者负载 (1.4ms) 选择“患者”。* 从“患者”中选择“患者”。“remember_token” = 'X6MlhaRLFMoZRkYaGiojfA' LIMIT 1 => #1 ——
【解决方案2】:

试试这样:

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

通过这种方式,您将能够知道您的令牌是否正在请求中。希望它会有所帮助。谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    相关资源
    最近更新 更多