2011-09-30 72 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。