ruby-on-rails - 连接到 127.0.0.1 上的 Redis 时出错 :6379(ECONNREFUSED) on rails application

标签 ruby-on-rails ruby redis

通过使用 Redis,我尝试在 Ruby on Rails 上创建排名系统。

我的环境:windows10, vagrant, Ruby -v 2.2.3, rails -v 4.2.5, mysql2 -v 0.3.18...

但是我得到了以下错误。

在 127.0.0.1:6379 (ECONNREFUSED) 上连接到 Redis 时出错

this is error pic on rails application...

我猜不是 app/posts_controller.rb 中的代码有问题,而是我的其他设置文件有问题。

这就是为什么当我在 ruing redis-server,我得到了输出没有任何问题。

以下与我的 Rails 应用程序相关的文件:

gemfile.lock

首先,我运行 bundle install gem 'redis'gem 'redis-rails'

GEM
  remote: https://rubygems.org/
  specs:
    actionmailer (4.2.5)
      actionpack (= 4.2.5)
      actionview (= 4.2.5)
      activejob (= 4.2.5)
      mail (~> 2.5, >= 2.5.4)
      rails-dom-testing (~> 1.0, >= 1.0.5)
    actionpack (4.2.5)
      actionview (= 4.2.5)
      activesupport (= 4.2.5)
      rack (~> 1.6)
      rack-test (~> 0.6.2)
      rails-dom-testing (~> 1.0, >= 1.0.5)
      rails-html-sanitizer (~> 1.0, >= 1.0.2)
    actionview (4.2.5)
      activesupport (= 4.2.5)
      builder (~> 3.1)
      erubis (~> 2.7.0)
      rails-dom-testing (~> 1.0, >= 1.0.5)
      rails-html-sanitizer (~> 1.0, >= 1.0.2)
    activejob (4.2.5)
      activesupport (= 4.2.5)
      globalid (>= 0.3.0)
    activemodel (4.2.5)
      activesupport (= 4.2.5)
      builder (~> 3.1)
    activerecord (4.2.5)
      activemodel (= 4.2.5)
      activesupport (= 4.2.5)
      arel (~> 6.0)
    activesupport (4.2.5)
      i18n (~> 0.7)
      json (~> 1.7, >= 1.7.7)
      minitest (~> 5.1)
      thread_safe (~> 0.3, >= 0.3.4)
      tzinfo (~> 1.1)
    arel (6.0.4)
    bcrypt (3.1.12)
    binding_of_caller (0.8.0)
      debug_inspector (>= 0.0.1)
    builder (3.2.3)
    byebug (10.0.2)
    coderay (1.1.2)
    coffee-rails (4.1.1)
      coffee-script (>= 2.2.0)
      railties (>= 4.0.0, < 5.1.x)
    coffee-script (2.4.1)
      coffee-script-source
      execjs
    coffee-script-source (1.12.2)
    concurrent-ruby (1.0.5)
    crass (1.0.4)
    debug_inspector (0.0.3)
    devise (4.5.0)
      bcrypt (~> 3.0)
      orm_adapter (~> 0.1)
      railties (>= 4.1.0, < 6.0)
      responders
      warden (~> 1.2.3)
    erubis (2.7.0)
    execjs (2.7.0)
    globalid (0.4.1)
      activesupport (>= 4.2.0)
    hike (1.2.3)
    i18n (0.9.5)
      concurrent-ruby (~> 1.0)
    jbuilder (2.7.0)
      activesupport (>= 4.2.0)
      multi_json (>= 1.2)
    jquery-rails (4.3.3)
      rails-dom-testing (>= 1, < 3)
      railties (>= 4.2.0)
      thor (>= 0.14, < 2.0)
    json (1.8.6)
    loofah (2.2.2)
      crass (~> 1.0.2)
      nokogiri (>= 1.5.9)
    mail (2.7.0)
      mini_mime (>= 0.1.1)
    method_source (0.9.0)
    mini_mime (1.0.1)
    mini_portile2 (2.3.0)
    minitest (5.11.3)
    multi_json (1.13.1)
    mysql2 (0.3.18)
    nokogiri (1.8.4)
      mini_portile2 (~> 2.3.0)
    orm_adapter (0.5.0)
    pry (0.11.3)
      coderay (~> 1.1.0)
      method_source (~> 0.9.0)
    pry-rails (0.3.6)
      pry (>= 0.10.4)
    rack (1.6.10)
    rack-test (0.6.3)
      rack (>= 1.0)
    rails (4.2.5)
      actionmailer (= 4.2.5)
      actionpack (= 4.2.5)
      actionview (= 4.2.5)
      activejob (= 4.2.5)
      activemodel (= 4.2.5)
      activerecord (= 4.2.5)
      activesupport (= 4.2.5)
      bundler (>= 1.3.0, < 2.0)
      railties (= 4.2.5)
      sprockets-rails
    rails-deprecated_sanitizer (1.0.3)
      activesupport (>= 4.2.0.alpha)
    rails-dom-testing (1.0.9)
      activesupport (>= 4.2.0, < 5.0)
      nokogiri (~> 1.6)
      rails-deprecated_sanitizer (>= 1.0.1)
    rails-html-sanitizer (1.0.4)
      loofah (~> 2.2, >= 2.2.2)
    railties (4.2.5)
      actionpack (= 4.2.5)
      activesupport (= 4.2.5)
      rake (>= 0.8.7)
      thor (>= 0.18.1, < 2.0)
    rake (12.3.1)
    rdoc (4.3.0)
    redis (3.2.0)
    redis-actionpack (5.0.2)
      actionpack (>= 4.0, < 6)
      redis-rack (>= 1, < 3)
      redis-store (>= 1.1.0, < 2)
    redis-activesupport (5.0.7)
      activesupport (>= 3, < 6)
      redis-store (>= 1.3, < 2)
    redis-rack (2.0.4)
      rack (>= 1.5, < 3)
      redis-store (>= 1.2, < 2)
    redis-rails (5.0.2)
      redis-actionpack (>= 5.0, < 6)
      redis-activesupport (>= 5.0, < 6)
      redis-store (>= 1.2, < 2)
    redis-store (1.5.0)
      redis (>= 2.2, < 5)
    responders (2.4.0)
      actionpack (>= 4.2.0, < 5.3)
      railties (>= 4.2.0, < 5.3)
    sass (3.2.19)
    sass-rails (4.0.5)
      railties (>= 4.0.0, < 5.0)
      sass (~> 3.2.2)
      sprockets (~> 2.8, < 3.0)
      sprockets-rails (~> 2.0)
    sdoc (0.4.2)
      json (~> 1.7, >= 1.7.7)
      rdoc (~> 4.0)
    spring (2.0.2)
      activesupport (>= 4.2)
    sprockets (2.11.0)
      hike (~> 1.2)
      multi_json (~> 1.0)
      rack (~> 1.0)
      tilt (~> 1.1, != 1.3.0)
    sprockets-rails (2.3.3)
      actionpack (>= 3.0)
      activesupport (>= 3.0)
      sprockets (>= 2.8, < 4.0)
    thor (0.20.0)
    thread_safe (0.3.6)
    tilt (1.4.1)
    turbolinks (5.2.0)
      turbolinks-source (~> 5.2)
    turbolinks-source (5.2.0)
    tzinfo (1.2.5)
      thread_safe (~> 0.1)
    uglifier (4.1.19)
      execjs (>= 0.3.0, < 3)
    warden (1.2.7)
      rack (>= 1.0)
    web-console (2.3.0)
      activemodel (>= 4.0)
      binding_of_caller (>= 0.7.2)
      railties (>= 4.0)
      sprockets-rails (>= 2.0, < 4.0)

PLATFORMS
  ruby

DEPENDENCIES
  byebug
  coffee-rails (~> 4.1.0)
  devise
  jbuilder (~> 2.0)
  jquery-rails
  mysql2 (= 0.3.18)
  pry-rails
  rails (= 4.2.5)
  redis (= 3.2.0)
  redis-rails
  sass-rails (~> 4.0.2)
  sdoc (~> 0.4.0)
  spring
  sprockets (= 2.11.0)
  turbolinks
  uglifier (>= 1.3.0)
  web-console (~> 2.0)

BUNDLED WITH
   1.10.6

config/initializes/redis.rb

其次,我创建了这个文件。

require 'redis'

uri = URI.parse(ENV["REDIS"])
REDIS = Redis.new(host: uri.host, port: uri.port)

config/environments/development.rb

第三,我在最下面一行添加了ENV["REDIS"] = "localhost:6379"

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = false

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  # Asset digests allow you to set far-future HTTP expiration dates on all assets,
  # yet still be able to expire them through the digest params.
  config.assets.digest = true

  # Adds additional error checking when serving assets at runtime.
  # Checks for improperly declared sprockets dependencies.
  # Raises helpful error messages.
  config.assets.raise_runtime_errors = true

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true

  ENV["REDIS"] = "localhost:6379"
end

app/controller/posts_controller.rb

最后,我在def indexdef show 中描述了新代码。

ids = REDIS.zrevrange "排名", 0, -1 @ranking_posts = Post.where(id: ids)

REDIS.zincrby "排名", 1, "#{@post.id}"

顺便说一句,如果我没有描述上面的两个描述, 我的 Rails 应用程序当然有效。

class PostsController < ApplicationController

  def index
    @posts = Post.all.order(id: :desc)


    ids = REDIS.zrevrange "ranking", 0, -1
    #the order of posts ranking
    @ranking_posts = Post.where(id: ids)

  end


  def show
    @post = Post.find_by(id: params[:id])

    # update ranking
    REDIS.zincrby "ranking", 1, "#{@post.id}"
  end



  def new
  end

  def create
    Post.create(name: post_params[:name], text: post_params[:text], user_id: current_user.id)

    redirect_to("/")
  end

  def destroy
    post = Post.find(params[:id])
    if post.user_id == current_user.id
      post.destroy
    end
    redirect_to("/")

请告诉我如何解决这个错误...(;^;)

最佳答案

Rails 不会启动 redis 服务器。您在那里提供的配置只是设置 redis 客户端以与 redis 服务器通信。您必须安装并启动 redis,以便它可以连接到某些东西。

https://redislabs.com/ebook/appendix-a/a-3-installing-on-windows/a-3-2-installing-redis-on-window/

关于ruby-on-rails - 连接到 127.0.0.1 上的 Redis 时出错 :6379(ECONNREFUSED) on rails application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52374229/

相关文章:

ruby - 如何调用在不同上下文中获取 block 的 Proc?

ruby-on-rails - 在 Ruby 中查找文件名的扩展名

ruby - Redis存储地理信息,

redis - 给出 Redis 中特定位置的所有条目

c++ - 在hiredis中关闭连接?

ruby-on-rails - 无法验证无表类中的唯一性

mysql - ActiveRecord::InvalidForeignKey

heroku - 如何估计redis数据库的大小?

ruby-on-rails - 路由错误 - 没有路由匹配 [GET] "/games"

ruby-on-rails - 从 Elasticsearch::Model::Response::Records 到 Kaminari::PaginatableArray 的转换