简体   繁体   English

:条件声明和Ruby Rails

[英]:Condition statement and Ruby Rails

I'm trying to get these statements to work: 我正在尝试使这些语句起作用:

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

but I am getting the error: 但是我得到了错误:

undefined method `to_sym' for nil:NilClass

when I should be getting a match. 当我应该去比赛的时候。

An example input is: 输入示例为:

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

Where am I going wrong? 我要去哪里错了?


More info: 更多信息:

The table has three fields, the title, release date, and rating, and is very simple. 该表具有三个字段,标题,发行日期和评级,非常简单。 The options for rating are stated above in @all_ratings . 评分选项已在@all_ratings中说明。

It tells you that you @valid_ratings in nil 它告诉你@valid_ratings nil

You probably trying doing this? 您可能尝试这样做?

@valid_ratings = Rating.find(params["ratings"])

Rails 3.x: Rails 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')

I am not sure what you are trying to with :rating.upcase . 我不确定您正在尝试使用:rating.upcase Is there a variable named rating? 是否有一个名为rating的变量? :rating is a symbol. :rating是一个符号。 upcase is not a method on Symbol. upcase不是Symbol的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM