下面我在一个名为 Movie 的模型中创建了一个类方法,它应该返回一个数组:
def self.all_ratings
Array['G','PG','PG-13','R','NC-17']
end
在我的电影控制器中,我使用以下实例变量访问它:
@all_ratings = Movie.all_ratings
但是,当在我的索引视图中使用它时,我收到以下错误:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
我相信我正在正确地创建数组,但我可能是错的。有什么建议为什么会发生这些错误?
下面是使用@all_ratings 的视图:
%h1 All Movies
= form_tag movies_path, :method => :get do
Include:
- @all_ratings.each do |rating|
= rating
= check_box_tag "ratings[#{rating}]"
= submit_tag 'Refresh'
这是我在控制器中实现@all_ratings 的方式
class MoviesController < ApplicationController
@all_ratings = Movie.all_ratings