質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

2回答

564閲覧

ActiveRecord::RecordNotFound in ObservationsController#edit Couldn't find Observation with 'id'=2

Misa0

総合スコア2

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/09/10 15:55

編集2020/09/11 03:37

編集画面に遷移出来ていたが、他の機能を実装した後に上記エラーが出る様になった。

エラー文

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が必要なのかどうかがいまいち分からないです。初心者です、よろしくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

理由が分かりました。私のアプリのデータ形式が表テーブルに複数の@observation = Observation.find(params[:patient_id])のデータが有ったためだと思われます。
現在javascriptでjQueryやAjaxを用いての編集形式に変更しました。
回答して頂いたnecocoa様、考えて頂いた方々有り難うございました。

投稿2020/09/20 07:40

Misa0

総合スコア2

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

@observation = Observation.find(params[:id])こちらが正しいです。

他の機能を実装した後に上記エラーが出たわけではなく、新しくPatientを作りpatient_idが2になったためエラーになったのでしょう。

また、編集画面に遷移出来ていたとあるので、editが合ったと仮定しますが、
editアクションが無いですが誤って消したりしてませんか?

投稿2020/09/11 01:33

necocoa

総合スコア209

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Misa0

2020/09/11 03:42

ご回答ありがとうございます! 上記私の方でも試行していたのですが、変化無しです。 →新しくPatientを作りpatient_idが2になったためエラーになった このご意見を頂き再度考察し、views/observations/show.html.erb の <%= link_to "観察項目編集", edit_patient_observation_path %> パスの指定の仕方がおかしいのかなと思うのですが、色々試行するのですがうまくいきません。 仮説が間違っているのか、記述が違うのか、またご意見いただければ助かります。 よろしくお願い致します。
necocoa

2020/09/11 04:17

edit_patient_observation_pathには `:patient_id`と`:id`の2つのidが必要になります。 ``` <%= link_to "観察項目編集", edit_patient_observation_path(@patient, @observation) %> ``` このような形で引数に渡すとできます。 あとは`@patient` `@observation`この2つを用意する必要があるので showアクションにこの2つの変数を用意するコードを追加すれば解決ですね! できなかった場合、observations/show.html.erbのコードを貼っていただけると助かります!
Misa0

2020/09/12 23:16

有難うございます! なのですが次はNo route matches {:action=>"edit", :controller=>"observations", :id=>nil, :patient_id=>nil}, missing required keys: [:id, :patient_id]と言うエラーが出ました。 〜controllers/observations_controller.rb〜 def edit @patient = Patient.find(patient_id: params[:patient_id]) @observation = Observation.find(observation: params[:observation_id]) end 〜controllers/patients_controller.rb〜 def edit @patient = Patient.find(patient_id: params[:patient_id]) end を追記するのですが、変わらないですね。 因みにobservations/show.html.erbは既に貼っているのですが、何か不足な部分が有りますでしょうか? ご回答宜しく御願いいたします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問