我用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
答案 0 :(得分:1)
让我们假设你有一个场景,你没有得到"排序"和"评级"在params。这将导致
@ratings = nil
在视图页面中,您调用的方法包括?在@ratings上会抛出此错误。您可能无法在本地测试此方案。
下面的代码将修复错误。
if params.has_key?"ratings"
@ratings = params["ratings"].keys
else
@ratings = []
end
答案 1 :(得分:0)
@ratings
变量未初始化。也许它会在本地块中初始化:
if @@first
@ratings = []
@@first=false
end
@@first
是什么以及设置在哪里?