0

ror を使用してアプリを作成しました。アプリはローカル マシンで完全に動作しますが、展開するとクラッシュします。コントローラーの関連コードは次のとおりです。

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

ここにビューのコードがあります

%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
4

2 に答える 2

1

パラメータに「並べ替え」と「評価」が含まれないシナリオがあると仮定します。これにより、

@ratings = nil

メソッドを呼び出したときのビューページにインクルード?@ratingsを超えると、このエラーがスローされます。このシナリオをローカルでテストしていない可能性があります。

以下のコードはエラーを修正します。

if params.has_key?"ratings"
  @ratings = params["ratings"].keys 
else
  @ratings = []
end
于 2012-08-06T13:00:01.163 に答える
0

@ratings変数は初期化されていません。おそらく、このブロックでローカルに初期化されます。

  if @@first
    @ratings = []
    @@first=false
  end

それは何で@@first、どこに設定されていますか?

于 2012-08-06T12:59:58.587 に答える