在Rails 2.3,我總是用Rails的JSON注入
render :json => { :success => true, :data => @foobar}
以JSON數據發送到我的前端。在Rails 3中,我正在使用
respond_to :json
...
respond_with @foobar
但是我錯過了:我需要JSON結構內的'success'值。在Rails 3中將這些數據注入JSON響應的正確方法是什麼?
嗯,試過這種過,但我得到以下錯誤的結果:
SyntaxError (app/controllers/properties_controller.rb:13: syntax error, unexpected tASSOC, expecting '}'
respond_with { :success => true, :data => @property }
^
/app/controllers/properties_controller.rb:13: Can't assign to true
respond_with { :success => true, :data => @property }
^
app/controllers/properties_controller.rb:13: syntax error, unexpected tASSOC, expecting tCOLON2 or '[' or '.'
respond_with { :success => true, :data => @property }
Hmhm,也試過這個,但是我得到如下錯誤結果: – ctp 2010-10-21 08:15:31