質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Redis

Redisは、オープンソースのkey-valueデータストアで、NoSQLに分類されます。すべてのデータをメモリ上に保存するため、処理が極めて高速です。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Vagrant

Vagrantは、VirtualBox上の仮想マシンを コマンドラインから作成してくれるソフトウェアです。 ビルド環境など容易に構築が可能です。

Q&A

解決済

1回答

1918閲覧

Railsでredisに接続できない

.0m

総合スコア9

Redis

Redisは、オープンソースのkey-valueデータストアで、NoSQLに分類されます。すべてのデータをメモリ上に保存するため、処理が極めて高速です。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Vagrant

Vagrantは、VirtualBox上の仮想マシンを コマンドラインから作成してくれるソフトウェアです。 ビルド環境など容易に構築が可能です。

0グッド

0クリップ

投稿2019/12/27 04:25

編集2019/12/29 11:38

前提・実現したいこと

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のサーバーの再起動

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

gouf

2019/12/28 16:14

Redis のサーバはどこで立ち上げられていますか? Vagrant 環境内ですか? 外ですか?
.0m

2019/12/29 03:18

外で立ち上げていました 今度はvagrant内でredisをたちあげられないのですが、一歩先に進めました ありがとうございます
gouf

2019/12/29 10:27

質問本文はいつでも編集可能です。状況の変化に応じて追記・削除など随時編集してください
.0m

2019/12/29 11:39

気づきませんでした 編集しました ありがとうございます
guest

回答1

0

ベストアンサー

エラーの出ているr
def redis_access   REDIS.zincrby "comments", 1, self.id
ですが、REDISの前に全角空白が入っています

投稿2019/12/30 03:49

winterboum

総合スコア23347

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

.0m

2019/12/30 05:04

まったく気がつきませんでした 無事解決しました ありがとうございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問