我是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
这按预期工作:从PARAMS我可以检索doctor_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)不起作用。
有没有人可以找出为什么会发生这种情况?
在此先感谢。
SOLUTION:
首先感谢所有为您的线索,它指引我的解决方案。由于调试器宝石,我可以看到“真正”发送给Patient.find_by_remember_token(令牌)的令牌在某种程度上是错误的。我的意思是。我是通过
赶上服务器上的令牌把令牌=>这回“X6MlhaRLFMoZRkYaGiojfA”(正确的标记)
而是通过调试我意识到,所发送的真正的令牌
“\” X6MlhaRLFMoZRkYaGiojfA \“”这绝对是一个错误的,所以我修改了我在明年的方式卷曲查询:
ORIGINAL CURL: curl -X POST -d 'report[patient_token]="X6MlhaRLFMoZRkYaGiojfA"&repo
MODIFIED ONE: curl -X POST -d 'report[patient_token]=X6MlhaRLFMoZRkYaGiojfA&repo
然后它的作品...该死的5小时延迟。
谢谢大家!
确实票面[:patient_token]存在?什么是它的价值是什么? –
是的,值:“X6MlhaRLFMoZRkYaGiojfA” –
因此它清楚地表明这一remember_token没有患者在数据库中存在。 –