質問編集履歴

1

tasks_controller.rbを追加しました

2019/06/23 13:33

投稿

waruhime
waruhime

スコア12

test CHANGED
File without changes
test CHANGED
@@ -88,7 +88,113 @@
88
88
 
89
89
  ```
90
90
 
91
-
91
+ #####tasks_controller.rb
92
+
93
+ ```
94
+
95
+ class TasksController < ApplicationController
96
+
97
+ before_action :set_task, only: [:show, :edit, :update, :destroy]
98
+
99
+ def index
100
+
101
+ @tasks = current_user.tasks.order(created_at: :desc)
102
+
103
+ end
104
+
105
+
106
+
107
+ def show
108
+
109
+ end
110
+
111
+
112
+
113
+ def new
114
+
115
+ @task = Task.new
116
+
117
+ end
118
+
119
+
120
+
121
+ def edit
122
+
123
+ end
124
+
125
+
126
+
127
+ def update
128
+
129
+ task.update!(task_params)
130
+
131
+ redirect_to tasks_url, notice:"タスク『#{task.name}』を更新しました。"
132
+
133
+ end
134
+
135
+
136
+
137
+ def destroy
138
+
139
+ task.destroy
140
+
141
+ redirect_to tasks_url, notice:"タスク『#{task.name}」を削除しました。"
142
+
143
+ end
144
+
145
+
146
+
147
+ def create
148
+
149
+ @task = current_user.tasks.new(task_params)
150
+
151
+
152
+
153
+ if @task.save
154
+
155
+ redirect_to @task, notice: "タスク『#{@task.name}』を登録しました。"
156
+
157
+ else
158
+
159
+ render :new
160
+
161
+ end
162
+
163
+ end
164
+
165
+
166
+
167
+
168
+
169
+
170
+
171
+
172
+
173
+
174
+
175
+ private
176
+
177
+
178
+
179
+ def task_params
180
+
181
+ params.require(:task).permit(:name, :description)
182
+
183
+ end
184
+
185
+
186
+
187
+ def set_task
188
+
189
+ @task = current_user.tasks.find(params[:id])
190
+
191
+ end
192
+
193
+ end
194
+
195
+
196
+
197
+ ```
92
198
 
93
199
  ### 試したこと
94
200