編集画面に遷移出来ていたが、他の機能を実装した後に上記エラーが出る様になった。
エラー文
ruby
1ActiveRecord::RecordNotFound in ObservationsController#edit 2Couldn't find Observation with 'id'=2 3 4def set_observation 5 **@observation = Observation.find(params[:patient_id])** 6 end 7end
ruby
1class ObservationsController < ApplicationController 2 before_action :set_patient, only: [:new, :create, :edit, :update] 3 before_action :set_observation, only: [:edit, :update] 4 5 def new 6 @observation = Observation.new 7 end 8 9 def create 10 @observation = Observation.new(observation_params) 11 if @observation.save # バリデーションをクリアした時 12 redirect_to patient_path(@patient) 13 else 14 render :new # バリデーションに弾かれた時 15 end 16 end 17 18 def show 19 observations = Observation.where(patient_id: params[:patient_id]).to_a 20 @observations = observations.sort_by{|o| o.time.delete(":").to_i} 21 @data = @observations.map do |o| 22 if o.temperature.present? 23 [o.time, o.temperature] 24 end 25 end 26 @data2 = @observations.map do |o| 27 if o.pulse.present? 28 [o.time, o.pulse] 29 end 30 end 31 @data3 = @observations.map do |o| 32 if o.respiration.present? 33 [o.time, o.respiration] 34 end 35 end 36 @data4 = @observations.map do |o| 37 if o.high_blood_pressure.present? 38 [o.time, o.high_blood_pressure] 39 end 40 end 41 @data5 = @observations.map do |o| 42 if o.low_blood_pressure.present? 43 [o.time, o.low_blood_pressure] 44 end 45 end 46 47 end 48 49 def update 50 if @observation.update(observation_params) # バリデーションをクリアした時 51 redirect_to patient_observation_path(@observation) 52 else 53 render :edit # バリデーションに弾かれた時 54 end 55 end 56 57 private 58 59 def observation_params 60 params.require(:observation).permit(:temperature, :pulse, :respiration, :high_blood_pressure, :low_blood_pressure, :spo2, :food_intake, 61 :water_intake, :excresion, :ex_amount, :atten_sound, :atten_part, :sputum, :cough, :sleep, :time, :hainyou).merge(user_id: current_user.id, patient_id: params[:patient_id], user_name: current_user.name) 62 end 63 64 def set_patient 65 @patient = Patient.find(params[:patient_id]) 66 end 67 68 def set_observation 69 @observation = Observation.find(params[:patient_id]) 70 end 71end
ruby
1Rails.application.routes.draw do 2 devise_for :users 3 get 'patients/index' 4 root to: "patients#index" 5 resources :users, only: [:edit, :update] 6 resources :patients, only: [:new, :create, :index, :show, :edit, :update] do 7 resources :observations, only: [:new, :create, :edit, :show, :update] 8 end 9end
ruby
1~views/observations/show.html.erb~ 2<div class="observation-show"> 3 <div class="observation-box"> 4 5 <div class="btn"> 6 <div class="ns-btn"> 7 <%= image_tag "くま看護師.png" ,class:"kuma-icon" ,width:"40",height:"40"%> 8 <%= link_to "観察項目編集", edit_patient_observation_path %> 9 </div> 10 <div class="kensa-btn"> 11 <%= image_tag "レントゲン1.jpg" ,class:"kensa-icon" ,width:"40",height:"40"%> 12 <%= link_to "検査結果参照", '#' %> 13 </div> 14 <div class="syoti-btn"> 15 <%= image_tag "点滴.jpeg" ,class:"treatment-icon" ,width:"40",height:"40"%> 16 <%= link_to "処置の入力", '#' %> 17 </div> 18 </div> 19 </div> 20</div>
def set_observation
@observation = Observation.find(params[:patient_id])
end
のfind(params[:patient_id])の:patient_idが必要なのかどうかがいまいち分からないです。初心者です、よろしくお願い致します。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。