Rail syntax "render json: @products"

I have been using rails on Windows for some time, and I decided to try it on Linux recently. Therefore, I set everything up, but now the project that I created on the windows does not work properly on ubuntu: it cannot interpret the following syntax:

render json: @products 

Produce the following error:

 /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 } 

And it only works if I change it:

 render :json => @products 

At first I thought it was because I was using an older version of ruby ​​(namely, 1.8.7). So I installed 1.9.2p290, but that didn't work.

If that matters, I use rails 3.1.0 and ubuntu 11.04.

Does anyone know what causes this? And how can I fix this? Thanks in advance!

+4
source share
2 answers

{ foo: 'bar' } is the new hash literal syntax introduced in Ruby 1.9 (not sure which release). Thus, it should (and work on my system) work with Ruby 1.9.2p290.

+2
source

Right!

 render :json => @products 

If you configure the as_json class as_json in your product model, everything you put in this hash will be included in the response at the JSON endpoint.

According to the official 3.1 manual, rails is the correct syntax .

+1
source

All Articles