开发者

rails syntax "render json: @products"

I've been using rails in windows for some time, and i've decided to try it on linux recently. So i've been setting everything up, but now the projec开发者_StackOverflow中文版t i had created on windows doesn't run properly on ubuntu: it fails to interpet the following syntax:

render json: @products

Producing 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 only works if i change it to:

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 it matters, i'm using rails 3.1.0 and ubuntu 11.04.

Does anyone know what's causing this? And how can i fix it? Thanks in advance!


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


The following is correct!

render :json => @products

If you setup a as_json class method in your Product model, whatever you place in that hash will be included in the response at your JSON endpoint.

As per the official 3.1 rails guide, this is the correct syntax.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜