前提・実現したいこと
プログラミング初心者です。railsでブログアプリを作っていてredisを使って記事のpvランキングをhomeに表示させたいです。
そこで、(https://qiita.com/kbdx/items/fdd36e895acf2532f0bd)の通りにredisをインストールし、(https://qiita.com/u651601f/items/a0624601047db4f02808)を参考にコードを書きました。そしてコマンドプロンプトからrails sを実行しましたがタイトルのようなエラーが表示され、手詰まりです。どなたかご教授よろしくお願いいたします。
発生している問題・エラーメッセージ
エラーメッセージ
該当のソースコード
gem
1Using rake 12.3.1 2Using concurrent-ruby 1.0.5 3Using minitest 5.11.3 4Using thread_safe 0.3.6 5Using builder 3.2.3 6Using erubi 1.7.1 7Using mini_portile2 2.3.0 8Using crass 1.0.4 9Using rack 2.0.5 10Using nio4r 2.3.1 11Using websocket-extensions 0.1.3 12Using mini_mime 1.0.1 13Using arel 8.0.0 14Using public_suffix 3.0.3 15Using bcrypt 3.1.12 (x86-mingw32) 16Using bindex 0.5.0 17Using bundler 1.15.3 18Using byebug 10.0.2 19Using ffi 1.9.25 (x86-mingw32) 20Using coffee-script-source 1.12.2 21Using execjs 2.7.0 22Using method_source 0.9.0 23Using thor 0.20.0 24Using orm_adapter 0.5.0 25Using multi_json 1.13.1 26Using pg 0.21.0 (x86-mingw32) 27Using puma 3.12.0 28Using rb-fsevent 0.10.3 29Using redis 4.0.2 30Using rubyzip 1.2.2 31Using tilt 2.0.8 32Using sqlite3 1.3.13 (x86-mingw32) 33Using turbolinks-source 5.2.0 34Using will_paginate 3.1.6 35Using i18n 1.1.0 36Using tzinfo 1.2.5 37Using nokogiri 1.8.4 (x86-mingw32) 38Using rack-test 1.1.0 39Using warden 1.2.7 40Using sprockets 3.7.2 41Using websocket-driver 0.6.5 42Using mail 2.7.0 43Using addressable 2.5.2 44Using childprocess 0.9.0 45Using rb-inotify 0.9.10 46Using coffee-script 2.4.1 47Using uglifier 4.1.19 48Using redis-store 1.5.0 49Using turbolinks 5.2.0 50Using activesupport 5.1.6 51Using tzinfo-data 1.2018.5 52Using loofah 2.2.2 53Using xpath 3.1.0 54Using selenium-webdriver 3.14.0 55Using sass-listen 4.0.0 56Using redis-rack 2.0.4 57Using rails-dom-testing 2.0.3 58Using globalid 0.4.1 59Using activemodel 5.1.6 60Using jbuilder 2.7.0 61Using redis-activesupport 5.0.7 62Using rails-html-sanitizer 1.0.4 63Using capybara 2.18.0 64Using sass 3.6.0 65Using activejob 5.1.6 66Using activerecord 5.1.6 67Using actionview 5.1.6 68Using actionpack 5.1.6 69Using actioncable 5.1.6 70Using actionmailer 5.1.6 71Using railties 5.1.6 72Using sprockets-rails 3.2.1 73Using redis-actionpack 5.0.2 74Using coffee-rails 4.2.2 75Using responders 2.4.0 76Using web-console 3.7.0 77Using rails 5.1.6 78Using sass-rails 5.0.7 79Using redis-rails 5.0.2 80Using devise 4.5.0
#config/environments/development.rb (中略) ENV["REDIS"] = "localhost:6379" end #config/initializers/test.rb require 'redis' uri = URI.parse(ENV["REDIS"]) REDIS = Redis.new(host: uri.host, port: uri.port) #app/controllers/microposts_controllers.rb before_action :authenticate_user!, only: [:create, :destroy] def create @micropost = current_user.microposts.build(micropost_params) if @micropost.save flash[:success] = "Micropost created!" redirect_to root_url else flash.now[:faild] = "Micropost hasn't created!" render microposts_path end end def destroy end def show @micro = Micropost.find(params[:id]) @article = Micropost.find(params[:id]) REDIS.zincrby "articles/daily", 1, @article.id end private def micropost_params params.require(:micropost).permit(:content,:title) end end #controllers/staticpages_controller.rb class StaticpagesController < ApplicationController def home @micropost = current_user.microposts.build if user_signed_in? @microposts = Micropost.all @daily_pageviews = Hash.new # 個別記事のPV数を取り出す @microposts.each do |post| @daily_pageviews[post.id] = REDIS.get "posts/#{post.id}" end # PV数のソーティング @lanks = @daily_pageviews.sort_by{|k, v| v} #上位3個の記事を返す @top3_pages = @lanks[0..3] end end #views/staticpages/home.html.erb (中略) <div class="col-md-8"> <ol class="microposts"> <% @top3_pages.each do |topic| %> <li><%= link_to topic[:title],topic %></li> <% end %> </ol> </div>
試したこと
initializersのredis.rbをtest.rbにrenameした。
補足情報(FW/ツールのバージョンなど)
win 10
rails 5.1.6
redis-x64-3.2.100
コマンドプロンプトを二つだして片方でredis-serverを立ち上げ、もう一方でrails sしました
長文失礼しました。回答お願いします。
あなたの回答
tips
プレビュー