質問編集履歴
1
ミスってました
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
railsでredisを使ってブログのpvランキングを表示させたい
|
test
CHANGED
@@ -24,13 +24,317 @@
|
|
24
24
|
|
25
25
|
### 該当のソースコード
|
26
26
|
|
27
|
-
|
27
|
+
```gem
|
28
|
+
|
28
|
-
|
29
|
+
Using rake 12.3.1
|
30
|
+
|
29
|
-
|
31
|
+
Using concurrent-ruby 1.0.5
|
32
|
+
|
30
|
-
|
33
|
+
Using minitest 5.11.3
|
34
|
+
|
35
|
+
Using thread_safe 0.3.6
|
36
|
+
|
37
|
+
Using builder 3.2.3
|
38
|
+
|
39
|
+
Using erubi 1.7.1
|
40
|
+
|
41
|
+
Using mini_portile2 2.3.0
|
42
|
+
|
43
|
+
Using crass 1.0.4
|
44
|
+
|
45
|
+
Using rack 2.0.5
|
46
|
+
|
47
|
+
Using nio4r 2.3.1
|
48
|
+
|
49
|
+
Using websocket-extensions 0.1.3
|
50
|
+
|
51
|
+
Using mini_mime 1.0.1
|
52
|
+
|
53
|
+
Using arel 8.0.0
|
54
|
+
|
55
|
+
Using public_suffix 3.0.3
|
56
|
+
|
57
|
+
Using bcrypt 3.1.12 (x86-mingw32)
|
58
|
+
|
59
|
+
Using bindex 0.5.0
|
60
|
+
|
61
|
+
Using bundler 1.15.3
|
62
|
+
|
63
|
+
Using byebug 10.0.2
|
64
|
+
|
65
|
+
Using ffi 1.9.25 (x86-mingw32)
|
66
|
+
|
67
|
+
Using coffee-script-source 1.12.2
|
68
|
+
|
69
|
+
Using execjs 2.7.0
|
70
|
+
|
71
|
+
Using method_source 0.9.0
|
72
|
+
|
73
|
+
Using thor 0.20.0
|
74
|
+
|
75
|
+
Using orm_adapter 0.5.0
|
76
|
+
|
77
|
+
Using multi_json 1.13.1
|
78
|
+
|
79
|
+
Using pg 0.21.0 (x86-mingw32)
|
80
|
+
|
81
|
+
Using puma 3.12.0
|
82
|
+
|
83
|
+
Using rb-fsevent 0.10.3
|
84
|
+
|
85
|
+
Using redis 4.0.2
|
86
|
+
|
87
|
+
Using rubyzip 1.2.2
|
88
|
+
|
89
|
+
Using tilt 2.0.8
|
90
|
+
|
91
|
+
Using sqlite3 1.3.13 (x86-mingw32)
|
92
|
+
|
93
|
+
Using turbolinks-source 5.2.0
|
94
|
+
|
95
|
+
Using will_paginate 3.1.6
|
96
|
+
|
97
|
+
Using i18n 1.1.0
|
98
|
+
|
99
|
+
Using tzinfo 1.2.5
|
100
|
+
|
101
|
+
Using nokogiri 1.8.4 (x86-mingw32)
|
102
|
+
|
103
|
+
Using rack-test 1.1.0
|
104
|
+
|
105
|
+
Using warden 1.2.7
|
106
|
+
|
107
|
+
Using sprockets 3.7.2
|
108
|
+
|
109
|
+
Using websocket-driver 0.6.5
|
110
|
+
|
111
|
+
Using mail 2.7.0
|
112
|
+
|
113
|
+
Using addressable 2.5.2
|
114
|
+
|
115
|
+
Using childprocess 0.9.0
|
116
|
+
|
117
|
+
Using rb-inotify 0.9.10
|
118
|
+
|
119
|
+
Using coffee-script 2.4.1
|
120
|
+
|
121
|
+
Using uglifier 4.1.19
|
122
|
+
|
123
|
+
Using redis-store 1.5.0
|
124
|
+
|
125
|
+
Using turbolinks 5.2.0
|
126
|
+
|
127
|
+
Using activesupport 5.1.6
|
128
|
+
|
129
|
+
Using tzinfo-data 1.2018.5
|
130
|
+
|
131
|
+
Using loofah 2.2.2
|
132
|
+
|
133
|
+
Using xpath 3.1.0
|
134
|
+
|
135
|
+
Using selenium-webdriver 3.14.0
|
136
|
+
|
137
|
+
Using sass-listen 4.0.0
|
138
|
+
|
139
|
+
Using redis-rack 2.0.4
|
140
|
+
|
141
|
+
Using rails-dom-testing 2.0.3
|
142
|
+
|
143
|
+
Using globalid 0.4.1
|
144
|
+
|
145
|
+
Using activemodel 5.1.6
|
146
|
+
|
147
|
+
Using jbuilder 2.7.0
|
148
|
+
|
149
|
+
Using redis-activesupport 5.0.7
|
150
|
+
|
151
|
+
Using rails-html-sanitizer 1.0.4
|
152
|
+
|
153
|
+
Using capybara 2.18.0
|
154
|
+
|
155
|
+
Using sass 3.6.0
|
156
|
+
|
157
|
+
Using activejob 5.1.6
|
158
|
+
|
159
|
+
Using activerecord 5.1.6
|
160
|
+
|
161
|
+
Using actionview 5.1.6
|
162
|
+
|
163
|
+
Using actionpack 5.1.6
|
164
|
+
|
165
|
+
Using actioncable 5.1.6
|
166
|
+
|
167
|
+
Using actionmailer 5.1.6
|
168
|
+
|
169
|
+
Using railties 5.1.6
|
170
|
+
|
171
|
+
Using sprockets-rails 3.2.1
|
172
|
+
|
173
|
+
Using redis-actionpack 5.0.2
|
174
|
+
|
175
|
+
Using coffee-rails 4.2.2
|
176
|
+
|
177
|
+
Using responders 2.4.0
|
178
|
+
|
179
|
+
Using web-console 3.7.0
|
180
|
+
|
181
|
+
Using rails 5.1.6
|
182
|
+
|
183
|
+
Using sass-rails 5.0.7
|
184
|
+
|
185
|
+
Using redis-rails 5.0.2
|
186
|
+
|
187
|
+
Using devise 4.5.0
|
188
|
+
|
31
|
-
```
|
189
|
+
```
|
190
|
+
|
191
|
+
|
192
|
+
|
32
|
-
|
193
|
+
```
|
194
|
+
|
195
|
+
#config/environments/development.rb
|
196
|
+
|
197
|
+
(中略)
|
198
|
+
|
199
|
+
ENV["REDIS"] = "localhost:6379"
|
200
|
+
|
201
|
+
end
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
#config/initializers/test.rb
|
206
|
+
|
207
|
+
require 'redis'
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
uri = URI.parse(ENV["REDIS"])
|
212
|
+
|
213
|
+
REDIS = Redis.new(host: uri.host, port: uri.port)
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
#app/controllers/microposts_controllers.rb
|
218
|
+
|
219
|
+
before_action :authenticate_user!, only: [:create, :destroy]
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
def create
|
226
|
+
|
227
|
+
@micropost = current_user.microposts.build(micropost_params)
|
228
|
+
|
229
|
+
if @micropost.save
|
230
|
+
|
231
|
+
flash[:success] = "Micropost created!"
|
232
|
+
|
233
|
+
redirect_to root_url
|
234
|
+
|
235
|
+
else
|
236
|
+
|
237
|
+
flash.now[:faild] = "Micropost hasn't created!"
|
238
|
+
|
239
|
+
render microposts_path
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
end
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
def destroy
|
248
|
+
|
249
|
+
end
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
def show
|
254
|
+
|
255
|
+
@micro = Micropost.find(params[:id])
|
256
|
+
|
257
|
+
@article = Micropost.find(params[:id])
|
258
|
+
|
259
|
+
REDIS.zincrby "articles/daily", 1, @article.id
|
260
|
+
|
261
|
+
end
|
262
|
+
|
33
|
-
|
263
|
+
private
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
def micropost_params
|
268
|
+
|
269
|
+
params.require(:micropost).permit(:content,:title)
|
270
|
+
|
271
|
+
end
|
272
|
+
|
273
|
+
end
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
#controllers/staticpages_controller.rb
|
278
|
+
|
279
|
+
class StaticpagesController < ApplicationController
|
280
|
+
|
281
|
+
def home
|
282
|
+
|
283
|
+
@micropost = current_user.microposts.build if user_signed_in?
|
284
|
+
|
285
|
+
@microposts = Micropost.all
|
286
|
+
|
287
|
+
@daily_pageviews = Hash.new
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
# 個別記事のPV数を取り出す
|
292
|
+
|
293
|
+
@microposts.each do |post|
|
294
|
+
|
295
|
+
@daily_pageviews[post.id] = REDIS.get "posts/#{post.id}"
|
296
|
+
|
297
|
+
end
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
# PV数のソーティング
|
302
|
+
|
303
|
+
@lanks = @daily_pageviews.sort_by{|k, v| v}
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
#上位3個の記事を返す
|
308
|
+
|
309
|
+
@top3_pages = @lanks[0..3]
|
310
|
+
|
311
|
+
end
|
312
|
+
|
313
|
+
end
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
#views/staticpages/home.html.erb
|
318
|
+
|
319
|
+
(中略)
|
320
|
+
|
321
|
+
<div class="col-md-8">
|
322
|
+
|
323
|
+
<ol class="microposts">
|
324
|
+
|
325
|
+
<% @top3_pages.each do |topic| %>
|
326
|
+
|
327
|
+
<li><%= link_to topic[:title],topic %></li>
|
328
|
+
|
329
|
+
<% end %>
|
330
|
+
|
331
|
+
</ol>
|
332
|
+
|
333
|
+
</div>
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
|
34
338
|
|
35
339
|
```
|
36
340
|
|
@@ -40,7 +344,7 @@
|
|
40
344
|
|
41
345
|
|
42
346
|
|
43
|
-
|
347
|
+
initializersのredis.rbをtest.rbにrenameした。
|
44
348
|
|
45
349
|
|
46
350
|
|
@@ -48,4 +352,14 @@
|
|
48
352
|
|
49
353
|
|
50
354
|
|
355
|
+
win 10
|
356
|
+
|
357
|
+
rails 5.1.6
|
358
|
+
|
359
|
+
redis-x64-3.2.100
|
360
|
+
|
361
|
+
コマンドプロンプトを二つだして片方でredis-serverを立ち上げ、もう一方でrails sしました![](901e0685c8e8cd3edd1e988b1f07d919.png)
|
362
|
+
|
363
|
+
|
364
|
+
|
51
|
-
|
365
|
+
長文失礼しました。回答お願いします。
|