ruby - Controller cannot access to table rails -
i'm newbie in rails , i'm developing app 3 controllers/models: doctors, patients , reports. doctor has many patients, patient belong doctor, patient has many reports , reports belongs patient.
to create patient outside through api have in controller:
def create if doc=params[:patient] doctor_id=doc[:doctor] else puts 'no params' =># monitor status in 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 expected: params can retrieve doctor_id , create new patient related him.
but strange thing came out when same reports. in controller have:
def create par=params[:report] token=par[:patient_token] pat=patient.find_by_remember_token(token) puts pat =>#this monitor server last_report=pat.reports.last puts last_report =>#this monitor 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 time server crashes , don't retrieve patient. pat=nil pat=patient.find_by_remember_token(token) don't works.
does can figure out why happening?
thanks in advance.
solution:
first of clues guide me solution. debugger gem see token "really" sent patient.find_by_remember_token(token) wrong in manner. mean. catching token on server through
puts token => return "x6mlharlfmozrkyagiojfa" (correct token)
but through debugger realize real token sent was
"\"x6mlharlfmozrkyagiojfa\"" wrong 1 modified 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 works... damn 5 hours of delay.
thanks all!!
try use searching patient.where(:remember_token => token)
should work.
Comments
Post a Comment