質問編集履歴

2

書式の改善

2021/03/23 08:58

投稿

jackson
jackson

スコア2

test CHANGED
File without changes
test CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
  これは@tagsがnilということでしょうか?
18
18
 
19
- ちなみにテーブル構造は投稿タグ付機能が多対多の構造になっています。
19
+ ちなみにテーブル構造は`question`モデル`tag`モデルが多対多の構造になっています。
20
20
 
21
21
 
22
22
 
@@ -152,7 +152,7 @@
152
152
 
153
153
  <div class="field" id="checkbox">
154
154
 
155
- <% @tags.each do |t| %>
155
+ <% @tags.each do |t| %> <-- ここでエラーになります。
156
156
 
157
157
  <%= form.label t.name %>
158
158
 
@@ -218,20 +218,20 @@
218
218
 
219
219
  def new
220
220
 
221
+ @tags = Tag.all <--全てのタグを取ってくる
222
+
223
+ @question = Question.new
224
+
225
+ end
226
+
227
+
228
+
229
+ # GET /questions/1/edit
230
+
231
+ def edit
232
+
221
233
  @tags = Tag.all
222
234
 
223
- @question = Question.new
224
-
225
- end
226
-
227
-
228
-
229
- # GET /questions/1/edit
230
-
231
- def edit
232
-
233
- @tags = Tag.all
234
-
235
235
  end
236
236
 
237
237
 

1

routes.rbの追加、questionコントローラの修正、字崩れの修正

2021/03/23 08:58

投稿

jackson
jackson

スコア2

test CHANGED
File without changes
test CHANGED
@@ -50,10 +50,14 @@
50
50
 
51
51
  end
52
52
 
53
+ ```
54
+
53
55
 
54
56
 
55
57
  ####app/models/question_tag.rb(中間テーブル)
56
58
 
59
+
60
+
57
61
  ```
58
62
 
59
63
  class QuestionTag < ApplicationRecord
@@ -70,6 +74,8 @@
70
74
 
71
75
  ####app/models/tag.rb
72
76
 
77
+
78
+
73
79
  ```
74
80
 
75
81
  class Tag < ApplicationRecord
@@ -86,7 +92,7 @@
86
92
 
87
93
 
88
94
 
89
- ```
95
+
90
96
 
91
97
  #####app/views/questions/new.html.erb
92
98
 
@@ -220,6 +226,86 @@
220
226
 
221
227
 
222
228
 
229
+ # GET /questions/1/edit
230
+
231
+ def edit
232
+
233
+ @tags = Tag.all
234
+
235
+ end
236
+
237
+
238
+
239
+ # POST /questions or /questions.json
240
+
241
+ def create
242
+
243
+ @question = current_user.questions.build(question_params)
244
+
245
+ respond_to do |format|
246
+
247
+ if @question.save
248
+
249
+ format.html { redirect_to @question, notice: "質問を投稿しました" }
250
+
251
+ format.json { render :show, status: :created, location: @question }
252
+
253
+ else
254
+
255
+ format.html { render :new, status: :unprocessable_entity }
256
+
257
+ format.json { render json: @question.errors, status: :unprocessable_entity }
258
+
259
+ end
260
+
261
+ end
262
+
263
+ end
264
+
265
+
266
+
267
+ # PATCH/PUT /questions/1 or /questions/1.json
268
+
269
+ def update
270
+
271
+ respond_to do |format|
272
+
273
+ if @question.update(question_params)
274
+
275
+ format.html { redirect_to @question, notice: "更新しました" }
276
+
277
+ format.json { render :show, status: :ok, location: @question }
278
+
279
+ else
280
+
281
+ format.html { render :edit, status: :unprocessable_entity }
282
+
283
+ format.json { render json: @question.errors, status: :unprocessable_entity }
284
+
285
+ end
286
+
287
+ end
288
+
289
+ end
290
+
291
+
292
+
293
+ # DELETE /questions/1 or /questions/1.json
294
+
295
+ def destroy
296
+
297
+ @question.destroy
298
+
299
+ respond_to do |format|
300
+
301
+ format.html { redirect_to questions_url, notice: "質問を削除しました" }
302
+
303
+ format.json { head :no_content }
304
+
305
+ end
306
+
307
+ end
308
+
223
309
 
224
310
 
225
311
  private
@@ -250,6 +336,74 @@
250
336
 
251
337
 
252
338
 
339
+ #routes.rb
340
+
341
+
342
+
343
+ ```
344
+
345
+ Rails.application.routes.draw do
346
+
347
+
348
+
349
+ devise_for :admin_users, ActiveAdmin::Devise.config
350
+
351
+ ActiveAdmin.routes(self)
352
+
353
+ resources :reactions
354
+
355
+ get 'answers' => 'answers#index'
356
+
357
+ resources :questions, shallow: true do
358
+
359
+ resources :answers, shallow: true do
360
+
361
+ resources :reactions
362
+
363
+ end
364
+
365
+ end
366
+
367
+ devise_for :users, controllers: { omniauth_callbacks: 'omniauth_callbacks' ,
368
+
369
+ registrations: 'users/registrations',
370
+
371
+ sessions: 'users/sessions'
372
+
373
+ }
374
+
375
+ get 'users/show', to: 'users#show'
376
+
377
+ resources :users
378
+
379
+ resource :user, except: [:new, :create, :destroy]
380
+
381
+ root 'questions#index'
382
+
383
+ get 'pages/index'
384
+
385
+ get 'pages/show'
386
+
387
+ get 'questions', to:'questions#index'
388
+
389
+ get 'static_pages/home'
390
+
391
+ end
392
+
393
+
394
+
395
+ ```
396
+
397
+
398
+
253
399
  ##試してみたこと
254
400
 
255
401
  `question`モデルと`タグ`モデルが多対多なのでそれが関係しているのかなと思い探してはみましたが見つかりませんでした。
402
+
403
+
404
+
405
+ ###追記
406
+
407
+ routes.rbとquestionモデルを修正しました。
408
+
409
+ つけるとエラーが起こるvalidationは`:title`と`:body`です