在我的 Rails 应用程序中,我需要检查一个对象是否存在,如果存在,我需要将该对象的纬度和经度属性分配给两个名为纬度和经度的外部变量。
my controller code
def query
if ( Coordinates.where(city :params[:show]).exists?) equal? 1) then
a=Coordinates.where(city: params[:show])
latitude=a.latitude
longitude=a.longitude
end
但是当我在浏览器中运行程序时,尽管我多次尝试更改语法,但我收到语法错误我收到的错误是“语法错误,意外的 tFID,期望关键字_then 或 ';'或“\n””。任何人请帮助我如何解决这个问题,提前致谢
最佳答案
我有几句话:
在 Ruby 中,if 的语法通常是(没有 then)
if condition code end
where 返回一个 ActiveRecord 关联,因此它是一个集合,因此您不能调用
a.latitude
等。尝试Cooperatives.where(city: params[:show]).first
或类似的东西。您定义了一个名为
equal 的方法吗?
?如果没有,至少缺少一个.
。if 语句或多或少没用。
def query a = Coordinates.where(city: params[:show]) b = a.first if a.count == 1 latitude = b.latitude longitude= b.longitude else # do something else end end
关于ruby-on-rails-3 - 语法错误 "syntax error, unexpected tFID, expecting keyword_then or ' ;' or '\n' “检查对象是否存在时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17896309/