Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails语法;呈现json:@products";_Ruby On Rails_Ruby_Ruby On Rails 3_Ruby On Rails 3.1 - Fatal编程技术网

Ruby on rails rails语法;呈现json:@products";

Ruby on rails rails语法;呈现json:@products";,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.1,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.1,我在windows中使用rails已经有一段时间了,最近我决定在linux上试用它。所以我一直在设置一切,但现在我在windows上创建的项目在ubuntu上无法正常运行:它无法解释以下语法: render json: @products 产生以下错误: /home/dcastro/workspace/teste/app/controllers/products_controller.rb:9: syntax error, unexpected ':', expecting '}' for

我在windows中使用rails已经有一段时间了,最近我决定在linux上试用它。所以我一直在设置一切,但现在我在windows上创建的项目在ubuntu上无法正常运行:它无法解释以下语法:

render json: @products
产生以下错误:

/home/dcastro/workspace/teste/app/controllers/products_controller.rb:9: syntax error, unexpected ':', expecting '}'
  format.json { render json: @products }
                            ^
/home/dcastro/workspace/teste/app/controllers/products_controller.rb:20: syntax error, unexpected ':', expecting '}'
  format.json { render json: @product }
仅当我将其更改为:

render :json => @products
起初我认为这是因为我使用的是旧版本的ruby(即1.8.7)。所以我安装了1.9.2p290,但那不起作用

如果有必要的话,我使用的是rails 3.1.0和ubuntu 11.04


有人知道这是什么原因吗?我怎样才能修好它?提前谢谢

以下是正确的

render :json => @products
如果在产品模型中设置
as_json
类方法,则在该散列中放置的任何内容都将包含在json端点的响应中


根据官方的3.1 rails指南,.

{foo:'bar'}
是Ruby 1.9中引入的新哈希文本语法(不确定是哪个版本)。因此,它应该(并且在我的系统上)与Ruby 1.9.2p290一起工作。

您是否尝试过旧语法
render:json=>@products
?我尝试过,并且在我的帖子中指出了这一点。我发现了问题所在:即使我安装了1.9.2,服务器仍在1.8上运行。因此,我卸载了ruby1.8,并为ruby1.9.2创建了一个名为“ruby1.8”的符号链接,诱使服务器在ruby1.9.2上运行。