質問編集履歴
4
追加情報
test
CHANGED
File without changes
|
test
CHANGED
@@ -24,10 +24,16 @@
|
|
24
24
|
|
25
25
|
|
26
26
|
|
27
|
+
追記:考えた結果、削除を実行した際にモデルのデータからは情報が削除されて、一方でRedisからはデータが削除されていないためエラーになることが分かりました。そこで下記のコードをpostsコントローラのdestroyアクションに追加したのですが、再びエラーが発生しています。
|
28
|
+
|
29
|
+
|
30
|
+
|
27
31
|
|
28
32
|
|
29
33
|
### 発生している問題・エラーメッセージ
|
30
34
|
|
35
|
+
destroyアクション等にコードを追加するまでは下のエラーが発生していました。
|
36
|
+
|
31
37
|
|
32
38
|
|
33
39
|
```
|
@@ -36,17 +42,65 @@
|
|
36
42
|
|
37
43
|
```
|
38
44
|
|
45
|
+
そしてランキングデータを取得するアクションの下の行を
|
46
|
+
|
47
|
+
@ranking_posts = ids.map{| id |Post.find(id)}
|
48
|
+
|
49
|
+
次のように書き換えました。
|
50
|
+
|
51
|
+
@ranking_posts = ids.map{| id |Post.find_by(id: id)}
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
また、Redisのソート済セット型のデータから日付をキー、ポストのIdをメンバーを指定してスコアを削除するようにpostsコントローラーのdestroyアクションに下の行を追加しました(@postはビフォーアクションで取得できています。)
|
56
|
+
|
57
|
+
REDIS.zrem "posts/daily/#{Date.today.to_s}", @post.id
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
そしてその結果発生しているエラーが下です。
|
62
|
+
|
63
|
+
```
|
64
|
+
|
65
|
+
undefined method `user_id' for nil:NilClass
|
66
|
+
|
67
|
+
```
|
68
|
+
|
69
|
+
エラーの該当箇所は間違ったユーザーに編集削除を許可するためのアクションです。
|
70
|
+
|
71
|
+
```
|
72
|
+
|
73
|
+
def ensure_correct_user
|
74
|
+
|
75
|
+
@post = Post.find_by(id: params[:id])
|
76
|
+
|
77
|
+
if @post.user_id != current_user.id
|
78
|
+
|
79
|
+
flash[:notice] = "Not yours"
|
80
|
+
|
81
|
+
redirect_to "/"
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
```
|
88
|
+
|
89
|
+
|
90
|
+
|
39
91
|
|
40
92
|
|
41
93
|
### 該当のソースコード
|
42
94
|
|
95
|
+
下はHomeコントローラーです。
|
96
|
+
|
43
97
|
|
44
98
|
|
45
99
|
```Ruby
|
46
100
|
|
47
101
|
class HomeController < ApplicationController
|
48
102
|
|
49
|
-
before_action :set_ranking_data
|
103
|
+
before_action :set_ranking_data, only:[:top]
|
50
104
|
|
51
105
|
|
52
106
|
|
@@ -64,9 +118,7 @@
|
|
64
118
|
|
65
119
|
ids = REDIS.zrevrangebyscore "posts/daily/#{Date.today.to_s}", "+inf", 0, limit: [0, 5]
|
66
120
|
|
67
|
-
# この下の行がエラーを発生させています
|
68
|
-
|
69
|
-
@ranking_posts = ids.map{| id |Post.find(id)}
|
121
|
+
@ranking_posts = ids.map{| id |Post.find_by(id: id)}
|
70
122
|
|
71
123
|
end
|
72
124
|
|
@@ -74,7 +126,7 @@
|
|
74
126
|
|
75
127
|
```
|
76
128
|
|
77
|
-
|
129
|
+
下はPostsコントローラーです。
|
78
130
|
|
79
131
|
|
80
132
|
|
@@ -88,7 +140,7 @@
|
|
88
140
|
|
89
141
|
before_action :ensure_correct_user, only:[:edit, :update, :destroy]
|
90
142
|
|
91
|
-
before_action :set_ranking_data
|
143
|
+
before_action :set_ranking_data, only:[:show, :edit]
|
92
144
|
|
93
145
|
|
94
146
|
|
@@ -124,11 +176,157 @@
|
|
124
176
|
|
125
177
|
ids = REDIS.zrevrangebyscore "posts/daily/#{Date.today.to_s}", "+inf", 0, limit: [0, 5]
|
126
178
|
|
127
|
-
@ranking_posts = ids.map{| id |Post.find(id)}
|
179
|
+
@ranking_posts = ids.map{| id |Post.find_by(id: id)}
|
128
|
-
|
180
|
+
|
129
|
-
end
|
181
|
+
end
|
182
|
+
|
183
|
+
|
184
|
+
|
130
|
-
|
185
|
+
# GET /posts/new
|
186
|
+
|
131
|
-
|
187
|
+
def new
|
188
|
+
|
189
|
+
@post = Post.new
|
190
|
+
|
191
|
+
end
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
# GET /posts/1/edit
|
196
|
+
|
197
|
+
def edit
|
198
|
+
|
199
|
+
@post = Post.find_by(id: params[:id])
|
200
|
+
|
201
|
+
end
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
# POST /posts
|
206
|
+
|
207
|
+
# POST /posts.json
|
208
|
+
|
209
|
+
def create
|
210
|
+
|
211
|
+
@post = Post.new(post_params)
|
212
|
+
|
213
|
+
@post.user_id = current_user.id
|
214
|
+
|
215
|
+
respond_to do |format|
|
216
|
+
|
217
|
+
if @post.save
|
218
|
+
|
219
|
+
format.html { redirect_to @post, notice: 'Post was successfully created.' }
|
220
|
+
|
221
|
+
format.json { render :show, status: :created, location: @post }
|
222
|
+
|
223
|
+
else
|
224
|
+
|
225
|
+
format.html { render :new }
|
226
|
+
|
227
|
+
format.json { render json: @post.errors, status: :unprocessable_entity }
|
228
|
+
|
229
|
+
end
|
230
|
+
|
231
|
+
end
|
232
|
+
|
233
|
+
end
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
# PATCH/PUT /posts/1
|
238
|
+
|
239
|
+
# PATCH/PUT /posts/1.json
|
240
|
+
|
241
|
+
def update
|
242
|
+
|
243
|
+
respond_to do |format|
|
244
|
+
|
245
|
+
if @post.update(post_params)
|
246
|
+
|
247
|
+
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
|
248
|
+
|
249
|
+
format.json { render :show, status: :ok, location: @post }
|
250
|
+
|
251
|
+
else
|
252
|
+
|
253
|
+
format.html { render :edit }
|
254
|
+
|
255
|
+
format.json { render json: @post.errors, status: :unprocessable_entity }
|
256
|
+
|
257
|
+
end
|
258
|
+
|
259
|
+
end
|
260
|
+
|
261
|
+
end
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
# DELETE /posts/1
|
266
|
+
|
267
|
+
# DELETE /posts/1.json
|
268
|
+
|
269
|
+
def destroy
|
270
|
+
|
271
|
+
@post.destroy
|
272
|
+
|
273
|
+
respond_to do |format|
|
274
|
+
|
275
|
+
format.html { redirect_to "/", notice: 'Post was successfully destroyed.' }
|
276
|
+
|
277
|
+
format.json { head :no_content }
|
278
|
+
|
279
|
+
end
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
# この部分でRedisのデータからポストを削除したいと思っています
|
284
|
+
|
285
|
+
REDIS.zrem "posts/daily/#{Date.today.to_s}", @post.id
|
286
|
+
|
287
|
+
end
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
private
|
292
|
+
|
293
|
+
# Use callbacks to share common setup or constraints between actions.
|
294
|
+
|
295
|
+
def set_post
|
296
|
+
|
297
|
+
@post = Post.find_by(id: params[:id])
|
298
|
+
|
299
|
+
end
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
# Never trust parameters from the scary internet, only allow the white list through.
|
304
|
+
|
305
|
+
def post_params
|
306
|
+
|
307
|
+
params.require(:post).permit(:title, :content)
|
308
|
+
|
309
|
+
end
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
def ensure_correct_user
|
314
|
+
|
315
|
+
@post = Post.find_by(id: params[:id])
|
316
|
+
|
317
|
+
if @post.user_id != current_user.id
|
318
|
+
|
319
|
+
flash[:notice] = "Not yours"
|
320
|
+
|
321
|
+
redirect_to "/"
|
322
|
+
|
323
|
+
end
|
324
|
+
|
325
|
+
end
|
326
|
+
|
327
|
+
end
|
328
|
+
|
329
|
+
|
132
330
|
|
133
331
|
```
|
134
332
|
|
3
追加情報
test
CHANGED
File without changes
|
test
CHANGED
@@ -17,6 +17,12 @@
|
|
17
17
|
追記:Idが存在していないポストをコンソール上で作成したら一旦、**動くようにはなったのですがそれでも投稿を削除しようとすると同じエラーが発生してしまいます。**
|
18
18
|
|
19
19
|
**私の予測ですが、どうやら削除した瞬間にランキングに表示すべきポストが入った変数がおかしくなっている気がします。**そのままランキング表示から削除したポストは外したいのですが、それもどうやったらいいのか見当がつきません…。**引き続き自分でのリサーチは続けます。**
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
追記:案の定、データを全て削除するとランキングの一番目にあったポストのIdが見つからないと言う風にエラーが表示されました。原因はわかったのですが解決方法がいまだに掴めていません。
|
24
|
+
|
25
|
+
|
20
26
|
|
21
27
|
|
22
28
|
|
2
追加情報
test
CHANGED
File without changes
|
test
CHANGED
@@ -15,6 +15,8 @@
|
|
15
15
|
|
16
16
|
|
17
17
|
追記:Idが存在していないポストをコンソール上で作成したら一旦、**動くようにはなったのですがそれでも投稿を削除しようとすると同じエラーが発生してしまいます。**
|
18
|
+
|
19
|
+
**私の予測ですが、どうやら削除した瞬間にランキングに表示すべきポストが入った変数がおかしくなっている気がします。**そのままランキング表示から削除したポストは外したいのですが、それもどうやったらいいのか見当がつきません…。**引き続き自分でのリサーチは続けます。**
|
18
20
|
|
19
21
|
|
20
22
|
|
1
追加情報
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
Railsで作成したマイクロポストアプリにRedisを用いてランキング機能を実装しました。無事に記事タイトルとPV数の表示を完了することができたのですが、次に他人としてログインして他の投稿が編集削除出来ないか確認しようとしたところエラーが発生してしまいました。
|
5
|
+
Railsで作成したマイクロポストアプリにRedisを用いてランキング機能を実装しました。無事に記事タイトルとPV数の表示を完了することができたのですが、**次に他人としてログインして他の投稿が編集削除出来ないか確認しようとしたところエラーが発生してしまいました。**
|
6
6
|
|
7
7
|
|
8
8
|
|
@@ -10,7 +10,11 @@
|
|
10
10
|
|
11
11
|
|
12
12
|
|
13
|
-
Railsは一ヶ月間勉強しており、Redisに関してはほぼ素人です。Redisに関する下記のコードは理解が乏しいかと思います。どうしてもアプリを完成させたいので、助言をいただけたら幸いです。
|
13
|
+
Railsは一ヶ月間勉強しており、**Redisに関してはほぼ素人です。Redisに関する下記のコードは理解が乏しいかと思います。**どうしてもアプリを完成させたいので、助言をいただけたら幸いです。
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
追記:Idが存在していないポストをコンソール上で作成したら一旦、**動くようにはなったのですがそれでも投稿を削除しようとすると同じエラーが発生してしまいます。**
|
14
18
|
|
15
19
|
|
16
20
|
|