前提・実現したいこと
https://yu-kyoto2720.hatenablog.com/entry/2018/10/31/165815
このサイトを参考にしてpvランキングサイトを作っています
下にスクリーンショットで貼っているエラーを解消したいです
https://qiita.com/hgsgtk/items/7e4990ad76ddaa6875e6
このサイトを参考にredisを立ち上げようとしていますが、sudo /etc/initを実行するとコマンドが見つかりませんと言われます
sudo /etc/init.d/redis start、redis-cliを実行すると下のようになります
[vagrant@localhost kadai7]$ sudo /etc/init.d/redis start [vagrant@localhost kadai7]$ redis-cli 127.0.0.1:6379>
OSはmacで、vagrantのローカル環境です
http://0.0.0.0:3000/comments/11に接続した時のエラーです(解消したいエラー)
発生している問題・エラーメッセージ
該当のソースコード
redis.rb
require 'redis' uri = URI.parse('localhost:6379') REDIS = Redis.new(host: uri.host, port: uri.port)
comments_controller.rb
class CommentsController < ApplicationController def index @comments = Comment.all end def show @comment = Comment.find_by(id: params[:id]) @user = User.find_by(id: @comment.user_id) @comment.redis_access end before_action :authenticate_user, {only: [:new, :create, :edit, :update, :destroy]} def new end def create Comment.create(title: comment_params[:title], content: comment_params[:content],user_id: current_user.id) redirect_to("/comments/index") end def edit if Comment.find_by(id: params[:id]).user_id == current_user.id @comment = Comment.find_by(id: params[:id]) else flash[:notice] = "Not Yours" redirect_to("/comments/index") end end def update @comment = Comment.find_by(id: params[:id]) @comment.title = comment_params[:title] @comment.content = comment_params[:content] @comment.save redirect_to("/comments/index") end def destroy if Comment.find_by(id: params[:id]).user_id == current_user.id @comment = Comment.find_by(id: params[:id]) @comment.destroy redirect_to("/comments/index") else flash[:notice] = "Not Yours" redirect_to("/comments/index") end end private def comment_params params.permit(:title,:content) end end
comments.rb
class Comment < ApplicationRecord validates :user_id, {presence: true} def redis_access REDIS.zincrby "comments", 1, self.id end def redis_page_view REDIS.zscore("comments", self.id).floor end def Comment.most_popular(limit: 4) most_popular_ids = REDIS.zrevrangebyscore "comments", "+inf", 0, limit: [0, limit] where(id: most_popular_ids).sort_by{ |comment| most_popular_ids.index(comment.id.to_s) } end end
show.html.erb
<div id="title"> <div> <p><%= @comment.created_at %></p> <%= @user.email %> <h1><%= @comment.title %></h1> </div> </div> <div id="article_wrap"> <div id="content"><%= @comment.content %> </div> <div class="clear"></div> </div>
routes.rb
Rails.application.routes.draw do devise_for :users root 'comments#index' get "comments/test" => "comments#test" get 'comments/index' get "comments/new" => "comments#new" get "comments/:id" => "comments#show" post 'comments/create' => 'comments#create' get "comments/:id/edit" => "comments#edit" post "comments/:id/update" => "comments#update" post "comments/:id/destroy" => "comments#destroy" # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end
試したこと
redis、railsのサーバーの再起動
回答1件
あなたの回答
tips
プレビュー