2011-09-30 70 views
4

我一直在windows中使用rails一段时间,并且我决定在最近在linux上尝试它。所以我一直在设置了一切,但现在我已经在Windows上创建的项目不会在Ubuntu上正常运行:失败来解读以下语法:rails语法“渲染json:@products”

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 

起初我还以为是因为我用红宝石的旧版本(即1.8.7)。所以我安装了1.9.2p290,但没有奏效。

如果很重要,我使用rails 3.1.0和ubuntu 11.04。

有谁知道是什么原因造成的?我该如何解决它?提前致谢!

+0

你尝试过老语法'render:json => @products'吗? – Bohdan

+0

我做了,我在我的帖子中指出。 – dcastro

+1

我发现哪里出了什么问题:即使我安装了1.9.2,服务器正在1.8上运行。所以我卸载了ruby1.8,并为ruby1.9.2创建了一个名为“ruby1.8”的符号链接,欺骗服务器在ruby 1.9.2上运行。 – dcastro

回答

1

以下是正确的!

render :json => @products 

如果您安装在您的产品型号,无论你在散列放置as_json类的方法将被包含在你的JSON端点的响应。

根据官方3.1导轨指南,this is the correct syntax

2

{ foo: 'bar' }是在Ruby 1.9中引入的新哈希字面语法(不确定是哪个版本)。所以,它应该(并且在我的系统上)使用Ruby 1.9.2p290。