ruby-on-rails - Heroku上的代码崩溃在本地完美运行

标签 ruby-on-rails ruby heroku crash

我用ror制作了一个应用程序,该应用程序在本地计算机上可以正常运行,但是在部署时会崩溃
这是 Controller 的相关代码

def index
  #debugger
  if @@first
    @ratings = []
    @@first=false
  end

@all_ratings = Movie.ratings_list

if params.has_key?:sort
  #debugger
  (params["rating_sort"].nil?)?(@ratings = []):(@ratings = params["rating_sort"])
  @movies = Movie.order(params[:sort]).find_all_by_rating(@ratings)       #:all,:conditions=>{"rating"=>@rating}, :order=>params[:sort])
  @sort = params[:sort]
else

   @ratings = params["ratings"].keys if params.has_key?"ratings"
   @movies = Movie.find_all_by_rating(@ratings)
end

end

这是 View 的代码
%h1 All Movies
= form_tag movies_path, :method => :get do
 Include:
 -@all_ratings.each do |r|
  = r
  = check_box_tag "ratings[#{r}]","ratings[#{r}]", @ratings.include?(r)?true:false
 = submit_tag "Refresh" , :id=>"ratings_submit"

%table#movies
 %thead
  %tr
    %th{ :class => ( "hilite" if @sort=="title" ) }
      %a#title_header{:href =>movies_path({:sort => 'title',"rating_sort"=>@ratings})}  Movie Title
  %th Rating
  %th{:class=>("hilite" if @sort=="release_date")} 
    %a#release_date_header{ :href => movies_path({:sort => 'release_date',"rating_sort"=>@ratings}) }Release Date
  %th More Info
%tbody
- @movies.each do |movie|
  %tr
    %td= movie.title 
    %td= movie.rating
    %td= movie.release_date
    %td= link_to "More about #{movie.title}", movie_path(movie)
= link_to 'Add new movie', new_movie_path

这是来自heroku的错误日志
2012-08-06T12:32:01+00:00 app[web.1]: Started GET "/movies" for 111.68.102.115 at        2012-08-06 12:32:01 +0000
2012-08-06T12:32:01+00:00 app[web.1]:   Processing by MoviesController#index as HTML
2012-08-06T12:32:01+00:00 app[web.1]: Rendered movies/index.html.haml within layouts/application (1.2ms)
2012-08-06T12:32:01+00:00 app[web.1]:     5:     = r
2012-08-06T12:32:01+00:00 app[web.1]: Completed 500 Internal Server Error in 5ms
2012-08-06T12:32:01+00:00 app[web.1]:     6:     = check_box_tag "ratings[#{r}]","ratings[#{r}]", @ratings.include?(r)?true:false
2012-08-06T12:32:01+00:00 app[web.1]: 
2012-08-06T12:32:01+00:00 app[web.1]: ActionView::Template::Error (undefined method `include?' for nil:NilClass):
2012-08-06T12:32:01+00:00 app[web.1]:     3:   Include:
2012-08-06T12:32:01+00:00 app[web.1]:     8:   
2012-08-06T12:32:01+00:00 app[web.1]:     4:   -@all_ratings.each do |r|
2012-08-06T12:32:01+00:00 app[web.1]:     7:   = submit_tag "Refresh" , :id=>"ratings_submit"
2012-08-06T12:32:01+00:00 app[web.1]:     9: %table#movies
2012-08-06T12:32:01+00:00 app[web.1]:   app/views/movies/index.html.haml:6:in `block (2 levels) in _app_views_movies_index_html_haml___805194019313391138_23501340'
2012-08-06T12:32:01+00:00 app[web.1]:   app/views/movies/index.html.haml:4:in `each'
2012-08-06T12:32:01+00:00 app[web.1]:   app/views/movies/index.html.haml:4:in `block in _app_views_movies_index_html_haml___805194019313391138_23501340'
2012-08-06T12:32:01+00:00 app[web.1]:   app/views/movies/index.html.haml:2:in `_app_views_movies_index_html_haml___805194019313391138_23501340'
2012-08-06T12:32:01+00:00 app[web.1]: 
2012-08-06T12:32:01+00:00 app[web.1]: cache: [GET /movies] miss
2012-08-06T12:32:01+00:00 app[web.1]: 
2012-08-06T12:32:01+00:00 heroku[router]: GET cclpotatoes.herokuapp.com/movies dyno=web.1 queue=0 wait=0ms service=15ms status=500 bytes=728

最佳答案

让我们假设您有一个场景,其中没有获得参数的“排序”和“评分”。这将导致

@ratings = nil

您在 View 页面中调用的方法包括吗?如果超过@ratings,则会引发此错误。您可能未在本地测试此方案。

下面的代码将修复该错误。
if params.has_key?"ratings"
  @ratings = params["ratings"].keys 
else
  @ratings = []
end

关于ruby-on-rails - Heroku上的代码崩溃在本地完美运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11828533/

相关文章:

ruby-on-rails - 在 Rails 3.1 中,为什么当调用另一个模型时我的自定义验证器会引发 'NameError: uninitialized constant' ?

ruby-on-rails - 如何使用 iron_worker 在 iron.io 上要求 '' pg"gem

Ruby Net::HTTP 第二次请求超时

ruby - 根据每个嵌套数组的第一个元素对 Ruby 数组重新排序

Heroku buildpack - 安装 texlive 二进制包

ruby-on-rails - Rails:Capybara无法使用Devise夹具数据登录用户

ruby-on-rails - ruby rails : How can I specify runner script environment

ruby-on-rails - App Engine Cron 作业始终返回 HTTP 状态代码 301

heroku - 在 Heroku 上的 Play 框架中处理文件上传

heroku - 'heroku' 似乎不是 git 存储库