質問編集履歴

1

boards_controllerの追加

2021/04/11 10:21

投稿

danburudoa
danburudoa

スコア8

test CHANGED
File without changes
test CHANGED
@@ -134,6 +134,118 @@
134
134
 
135
135
  ```
136
136
 
137
+ ```
138
+
139
+ boards_controller.rb
140
+
141
+
142
+
143
+ class BoardsController < ApplicationController
144
+
145
+
146
+
147
+ def index
148
+
149
+ @boards = Board.includes(:user)
150
+
151
+ end
152
+
153
+
154
+
155
+ def new
156
+
157
+ @board = Board.new
158
+
159
+ end
160
+
161
+
162
+
163
+ def create
164
+
165
+ @board = Board.new(board_params)
166
+
167
+ if @board.save
168
+
169
+ redirect_to root_path
170
+
171
+ else
172
+
173
+ render :new
174
+
175
+ end
176
+
177
+ end
178
+
179
+
180
+
181
+ def show
182
+
183
+ @board = Board.find(params[:id])
184
+
185
+ @comment = Comment.new
186
+
187
+ @comments = @board.comments.includes(:user)
188
+
189
+ end
190
+
191
+
192
+
193
+ def edit
194
+
195
+ @board = Board.find(params[:id])
196
+
197
+ end
198
+
199
+
200
+
201
+ def update
202
+
203
+ @board = Board.find(params[:id])
204
+
205
+ @board.update(board_params)
206
+
207
+ if @board.save
208
+
209
+ redirect_to root_path
210
+
211
+ else
212
+
213
+ render :edit
214
+
215
+ end
216
+
217
+ end
218
+
219
+
220
+
221
+ def destroy
222
+
223
+ @board = Board.find(params[:id])
224
+
225
+ @board.destroy
226
+
227
+ redirect_to root_path
228
+
229
+ end
230
+
231
+
232
+
233
+ private
234
+
235
+
236
+
237
+ def board_params
238
+
239
+ params.require(:board).permit(:title,:text,:image).merge(user_id: current_user.id)
240
+
241
+ end
242
+
243
+ end
244
+
245
+ ```
246
+
247
+
248
+
137
249
  ###試したこと
138
250
 
139
251
  Couldn't find Comment with