質問編集履歴

3

logの情報を追加しました。

2021/02/12 02:01

投稿

YutoKubo
YutoKubo

スコア13

test CHANGED
@@ -1 +1 @@
1
- [rails]コメントが表示できない
1
+ log[rails]コメントが表示できない
test CHANGED
@@ -227,3 +227,71 @@
227
227
 
228
228
 
229
229
  ```
230
+
231
+
232
+
233
+ <log>(フォームに"こんにちは"と入力)
234
+
235
+ ```ここに言語を入力
236
+
237
+ Started POST "/communities/1/comments" for ::1 at 2021-02-12 10:57:55 +0900
238
+
239
+ (1.1ms) SELECT sqlite_version(*)
240
+
241
+ Processing by CommentsController#create as HTML
242
+
243
+ Parameters: {"authenticity_token"=>"yViCtrZkddHwZ2kBdEjieduEyXF/FAz0E75iBg4w03R4NYReqB4odspub7p6+pBJ64J0LMSaOIKSJvQNJnboXg==", "comment"=>{"content"=>"こんにちは"}, "commit"=>"コメントする", "community_id"=>"1"}
244
+
245
+ User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
246
+
247
+ ↳ app/controllers/comments_controller.rb:4:in `create'
248
+
249
+ (0.1ms) begin transaction
250
+
251
+ ↳ app/controllers/comments_controller.rb:7:in `create'
252
+
253
+ User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
254
+
255
+ ↳ app/controllers/comments_controller.rb:7:in `create'
256
+
257
+ (0.1ms) rollback transaction
258
+
259
+ ↳ app/controllers/comments_controller.rb:7:in `create'
260
+
261
+ Redirected to http://localhost:3000/communities/1
262
+
263
+ Completed 302 Found in 38ms (ActiveRecord: 1.1ms | Allocations: 5860)
264
+
265
+
266
+
267
+
268
+
269
+ Started GET "/communities/1" for ::1 at 2021-02-12 10:57:55 +0900
270
+
271
+ Processing by CommunitiesController#show as HTML
272
+
273
+ Parameters: {"id"=>"1"}
274
+
275
+ Community Load (0.9ms) SELECT "communities".* FROM "communities" WHERE "communities"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
276
+
277
+ ↳ app/controllers/communities_controller.rb:54:in `set_community'
278
+
279
+ Rendering communities/show.html.erb within layouts/application
280
+
281
+ User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
282
+
283
+ ↳ app/views/communities/show.html.erb:32
284
+
285
+ Comment Load (0.6ms) SELECT "comments".* FROM "comments" WHERE "comments"."community_id" = ? [["community_id", 1]]
286
+
287
+ ↳ app/views/communities/show.html.erb:43
288
+
289
+ Rendered communities/show.html.erb within layouts/application (Duration: 11.1ms | Allocations: 2534)
290
+
291
+ [Webpacker] Everything's up-to-date. Nothing to do
292
+
293
+ Completed 200 OK in 51ms (Views: 43.0ms | ActiveRecord: 1.7ms | Allocations: 12251)
294
+
295
+
296
+
297
+ ```

2

編集しました

2021/02/12 02:01

投稿

YutoKubo
YutoKubo

スコア13

test CHANGED
File without changes
test CHANGED
@@ -94,7 +94,7 @@
94
94
 
95
95
  def comment_params
96
96
 
97
- params.require(:comment).permit(:content)
97
+ params.require(:comment).permit(:content,:community_id)
98
98
 
99
99
  end
100
100
 

1

community_controllerを前文のせました

2021/02/11 15:49

投稿

YutoKubo
YutoKubo

スコア13

test CHANGED
File without changes
test CHANGED
@@ -110,9 +110,21 @@
110
110
 
111
111
  ```ここに言語を入力
112
112
 
113
+ class CommunitiesController < ApplicationController
114
+
115
+ before_action :set_community, only: [:edit,:show,:update]
116
+
117
+
118
+
113
- ,,,,,,
119
+ def index
120
+
114
-
121
+ @communities = Community.all
122
+
123
+ end
124
+
125
+
126
+
115
- def show
127
+ def show
116
128
 
117
129
  @comments = @community.comments #投稿詳細に関連付けてあるコメントを全取得
118
130
 
@@ -120,6 +132,98 @@
120
132
 
121
133
  end
122
134
 
135
+
136
+
137
+ def edit
138
+
139
+
140
+
141
+ @community = Community.find(params[:id])
142
+
143
+
144
+
145
+ end
146
+
147
+
148
+
123
- ,,,,,,,
149
+ def new
150
+
151
+ @community = Community.new
152
+
153
+ end
154
+
155
+
156
+
157
+ def create
158
+
159
+ @community = current_user.communities.build(community_params)
160
+
161
+ if @community.save
162
+
163
+ redirect_to community_path(@community), notice: "投稿に成功しました。"
164
+
165
+ else
166
+
167
+ render :new
168
+
169
+ end
170
+
171
+ end
172
+
173
+
174
+
175
+
176
+
177
+ def update
178
+
179
+ if @community.update(community_params)
180
+
181
+ redirect_to community_path, notice: "投稿を更新しました。"
182
+
183
+ else
184
+
185
+ render :edit
186
+
187
+ end
188
+
189
+ end
190
+
191
+
192
+
193
+ def destroy
194
+
195
+ community = Community.find(params[:id])
196
+
197
+ community.destroy
198
+
199
+ redirect_to communities_path, notice: "投稿を削除しました。"
200
+
201
+ end
202
+
203
+
204
+
205
+
206
+
207
+ private
208
+
209
+ def community_params
210
+
211
+ params.require(:community).permit(:title, :body, :image, :content)
212
+
213
+ end
214
+
215
+
216
+
217
+ def set_community
218
+
219
+ @community = Community.find(params[:id])
220
+
221
+ end
222
+
223
+
224
+
225
+ end
226
+
227
+
124
228
 
125
229
  ```