NoMethodError(未定义的方法`sismember'代表nil:NilClass)...试图在heroku上部署

时间:2015-05-17 17:34:04

标签: ruby-on-rails ruby heroku

我收到以下错误:

2015-05-17T17:25:40.349230+00:00 app[web.1]: Started GET "/movies/1" for 160.9.0.153 at 2015-05-17 17:25:40 +0000
2015-05-17T17:25:40.423697+00:00 app[web.1]: Completed 500 Internal Server Error in 67ms
2015-05-17T17:25:40.355931+00:00 app[web.1]: Processing by MoviesController#show as HTML
2015-05-17T17:25:40.425095+00:00 app[web.1]: 
2015-05-17T17:25:40.406984+00:00 app[web.1]:   Movie Load (2.4ms)  SELECT  "movies".* FROM "movies" WHERE "movies"."id" = $1 LIMIT 1  [["id", 1]]
2015-05-17T17:25:40.425098+00:00 app[web.1]: NoMethodError (undefined method `sismember' for nil:NilClass):
2015-05-17T17:25:40.425100+00:00 app[web.1]:   app/models/movie.rb:20:in `cart_action'
2015-05-17T17:25:40.425102+00:00 app[web.1]: 
2015-05-17T17:25:40.425101+00:00 app[web.1]:   app/controllers/movies_controller.rb:9:in `show'
2015-05-17T17:25:40.425104+00:00 app[web.1]: 
2015-05-17T17:25:41.951208+00:00 heroku[router]: at=info method=GET path="/movies/4" host=hidden-savannah-5835.herokuapp.com request_id=cbb36b63-06ee-4da0-992d-6d512d431ea4 fwd="160.9.0.153" dyno=web.1 connect=0ms service=17ms status=500 bytes=1714
2015-05-17T17:25:41.932183+00:00 app[web.1]: Started GET "/movies/4" for 160.9.0.153 at 2015-05-17 17:25:41 +0000
2015-05-17T17:25:41.941428+00:00 app[web.1]: Completed 500 Internal Server Error in 5ms
2015-05-17T17:25:41.935883+00:00 app[web.1]: Processing by MoviesController#show as HTML
2015-05-17T17:25:41.935891+00:00 app[web.1]:   Parameters: {"id"=>"4"}
2015-05-17T17:25:41.939396+00:00 app[web.1]:   Movie Load (2.2ms)  SELECT  "movies".* FROM "movies" WHERE "movies"."id" = $1 LIMIT 1  [["id", 4]]
2015-05-17T17:25:41.944520+00:00 app[web.1]: 
2015-05-17T17:25:41.944523+00:00 app[web.1]: NoMethodError (undefined method `sismember' for nil:NilClass):
2015-05-17T17:25:41.944525+00:00 app[web.1]:   app/models/movie.rb:20:in `cart_action'
2015-05-17T17:25:41.944526+00:00 app[web.1]:   app/controllers/movies_controller.rb:9:in `show'
2015-05-17T17:25:41.944528+00:00 app[web.1]: 
2015-05-17T17:25:41.944529+00:00 app[web.1]: 

movie.rb (型号)

class Movie < ActiveRecord::Base
  has_many :purchases
  has_many :buyers, through: :purchases

  before_save :embed_video_url

  def poster
    "http://ia.media-imdb.com/images/M/#{poster_url}"
  end

  def imdb
    "http://www.imdb.com/title/#{imdb_id}/"
  end

  def embed_video_url
    self.video_url = "//www.youtube.com/embed/#{video_url.split('v=')[1].split('&list')[0]}"
  end

  def cart_action(current_user_id)
    if $redis.sismember "cart#{current_user_id}", id
      "Remove from"
    else
      "Add to"
    end
  end
end

movies_controller.rb

class MoviesController < ApplicationController

  def index
    @movies = Movie.all
  end

  def show
    @movie = Movie.find(params[:id])
    @cart_action = @movie.cart_action current_user.try :id
  end

end

初始化/ redis.rb

if Rails.env.development?
$redis = Redis.new(:driver => :hiredis)
elsif Rails.env.production?
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:url => uri)
end

关于这个错误的奇怪之处在于应用程序在本地运行没有任何问题但是当我部署到heroku时我可以看到第一页但是当我点击一部电影或尝试登录时我得到错误。

任何帮助?

1 个答案:

答案 0 :(得分:0)

您似乎只在$redis内的开发中定义了initializers/redis.rb ...这可以解释为什么$redis在生产时cart_action方法中为零。您必须在$redis

中设置Rails.env.production?