0

我正在尝试使这些语句起作用:

@all_ratings = ["G","PG","PG-13","R"]
@valid_ratings = params["ratings"]
@movies = Movie.find(:all ,  :conditions => {@valid_ratings[:rating.upcase] => "1"}   )

但我收到错误:

undefined method `to_sym' for nil:NilClass

当我应该得到一场比赛时。

一个示例输入是:

"ratings"=>{"PG-13"=>"1"}

我哪里错了?


更多信息:

该表有标题、发布日期和评分三个字段,非常简单。评级选项在上面的 中进行了说明@all_ratings

4

2 回答 2

0

它告诉你,你@valid_ratingsnil

你可能正在尝试这样做?

@valid_ratings = Rating.find(params["ratings"])
于 2013-02-25T03:35:57.997 回答
0

导轨 3.x:

@all_ratings = ["G","PG","PG-13","R"]
@valid_ratings = params["ratings"]
# Is @valid_ratings the same as you example, "ratings"=>{"PG-13"=>"1"}?
# It would be easiest to pass a subset of @all_ratings such that the params
# get converted to something like this: "ratings"=>["G", "PG"]
Movie.where(:rating => valid_ratings).all
# SQL: SELECT * FROM movies WHERE rating IN ('G','PG')

我不确定你想做什么:rating.upcase。是否有一个名为 rating 的变量?:rating是一个符号。upcase不是符号上的方法。

于 2013-02-25T03:56:54.760 回答