gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:55:20 25 4
gpt4 key购买 nike

我用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com