質問編集履歴

1

コントローラー追加

2023/01/13 08:39

投稿

gomes_2222
gomes_2222

スコア90

test CHANGED
File without changes
test CHANGED
@@ -62,6 +62,38 @@
62
62
  ```
63
63
  実行すると、todosテーブルから、idが1のレコードを取得。
64
64
 
65
+ コントローラーは、下記のとおりです。
66
+
67
+ もともとはshowでidによるレコードの抽出をしてました。
68
+ target_idでレコードを抽出する用のtargetを新しく作りましたが、やはり下記コマンドではデフォルトのidによる取得となってました。
69
+
70
+ ```ここに言語を入力
71
+ curl -i localhost:3000/todos/1
72
+ ```
73
+
74
+ ```rails
75
+ class TodosController < ApplicationController
76
+ before_action :set_todo, only: %i[show]
77
+
78
+ def show
79
+ render json: @todo
80
+ end
81
+
82
+ def target
83
+ @todo = Todo.where(target_id: params[:target_id])
84
+ render json: @todo
85
+ end
86
+
87
+ private
88
+
89
+ def set_todo
90
+ @todo = Todo.find(params[:id])
91
+ end
92
+
93
+ end
94
+
95
+ ```
96
+
65
97
  ### 補足情報(FW/ツールのバージョンなど)
66
98
  Mysql
67
99
  ruby on rails