railsでredisを使って,
pv数順にソートしたランキングを作成したいのですが、「Got 'H' as initial reply byte. If you're in a forking environment, such as Unicorn, you need to connect to Redis after forking.
」が出てしまいます。
環境の設定の段階で間違えているのかもしれません
どこがおかしいのか教えていただえれば幸いです。
このサイトを参考にし、
以下のようにしました。
posts_controller.rb
1class PostsController < ApplicationController 2 def index 3 @posts = Post.all.order('created_at DESC') 4 5 @daily_pageviews = Hash.new 6 # 個別記事のPV数を取り出す 7 @posts.each do |post| 8 @daily_pageviews[post.id] = REDIS.get "posts/#{post.id}" 9 end 10 # PV数のソーティング 11 @daily_pageviews.sort_by{|k, v| v} 12 #上位10個の記事を返す 13 @top10_pages = @daily_pageviews[0..10] 14 15 end 16
↓ index.html.erb
index.html.erb
1<% @top10_pages.each do |top10_page| %> 2<%= top10_page.title %> 3<% end %> 4
cloud9で開発しており、ローカル環境で動かしたいです。
↓config/developmentに以下をを追加
ENV["REDIS"] = "http://localhost:8080"
config/initializers/redis.rbを作成し以下を追加
require 'redis' uri = URI.parse(ENV["REDIS"]) REDIS = Redis.new(host: uri.host, port: uri.port)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。